Показать сообщение отдельно
  #7 (permalink)  
Старый 11.07.2022, 21:42
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,135

Сообщение от NovichokJS
разве не надо тут делить на 1000 ?
ок.
const getDiff = (startDate, endDate) => {
  let date1 = new Date(startDate);
  let date2 = new Date(endDate);
  let difference = Math.abs(date1 - date2);
  let days = Math.trunc(difference / (1000 * 60 * 60 * 24));
  difference -= days * (1000 * 60 * 60 * 24)
  let hours = Math.trunc(difference / (1000 * 60 * 60));
  difference -= hours * (1000 * 60 * 60)
  let minutes = Math.trunc(difference /  (1000 * 60));
  difference -= minutes * (1000 * 60)
  let seconds = difference/1000;

  return `${days}d ${hours}h ${minutes}m ${seconds}s`;
};
console.log(getDiff(new Date(2022, 11, 18, 3, 25, 0), new Date(2022, 11, 9, 1, 2, 0)));
console.log(getDiff(new Date(2022, 11, 9, 1, 2, 0), new Date(2022, 11, 18, 3, 25, 0)));
Ответить с цитированием