Получить разницу двух дат
Мне нужно получить разницу в таком формате:
9d 21h 103m 80s Написал код:
const getDiff = (startDate, endDate) => {
let date1 = new Date(startDate);
let date2 = new Date(endDate);
let days = Math.round(Math.abs((date1 - date2) / (1000 * 60 * 60 * 24)));
let hours = Math.round(Math.abs((date1 - date2) / (1000 * 60 * 60)));
let minutes = Math.round(Math.abs((date1 - date2) / (1000 * 60)));
let seconds = Math.round(Math.abs((date1 - date2) / 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)));
Получается что некорректно.. |
NovichokJS,
:)
const getDiff = (startDate, endDate) => {
let date1 = new Date(startDate);
let date2 = new Date(endDate);
let difference = Math.abs(date1 - date2);
let days = Math.round(difference / (1000 * 60 * 60 * 24));
difference -= days * (1000 * 60 * 60 * 24)
let hours = Math.round(difference / (1000 * 60 * 60));
difference -= hours * (1000 * 60 * 60)
let minutes = Math.round(difference / (1000 * 60));
difference -= minutes * (1000 * 60)
let seconds = difference;
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)));
|
Цитата:
что-то не то(( |
NovichokJS,
что не так? |
Цитата:
разве не надо тут делить на 1000 ? |
а нельзя решить с помощью методов getHours() и т.п? вместо того чтобы употреблять вот это hours * (1000 * 60 * 60) ?
|
Цитата:
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)));
|
хух, проблема была в том что тут let seconds = difference/1000; был пропущен Math.trunc
|
Рони, спасибо!
|
Альтертанивный вариант (извиняюсь что без новомодных let и =>)
function getDiff(startDate, endDate) {
// var date1 = new Date(startDate);
// var date2 = new Date(endDate);
// var diff = Math.abs(date2-date1);
var diff = Math.abs(new Date(endDate) - new Date(startDate));
diff = (diff-(diff%1000))/1000;
var seconds = diff % 60;
diff = (diff-seconds)/60;
var minutes = diff % 60;
diff = (diff-minutes)/60;
var hours = diff % 24;
var days = (diff-hours)/24;
return days+"d "+hours+"h "+minutes+"m "+seconds+"s";
}
|
| Часовой пояс GMT +3, время: 20:13. |