Reputation: 1272
I'd like to know how I can work out the calendar days between 2 dates. Currently I'm converting to timestamps and working out the diff as this seems to be the most common solution as below:
let time = 1561978969653;
let now = new Date();
let uploadDate = new Date(time);
let timeDiff = Math.abs(now.getTime() - uploadDate.getTime());
let days = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log(timeDiff, days);
However this has certain drawbacks. For example 26/01/2019 @ 11:59:59
to 27/01/2019 00:00:00
should count as 1 calendar day apart, however with the method I'm using it counts as 0 because it didn't have a full 24 hours between the 2 timestamps.
I have also thought about looping through the days and incrementing a counter but that seems a little inefficient and/or inelegant.
Edit: I do not want to implement a 3rd party library for this, I'd like a programatic solution, likewise timestamp based approaches do not resolve the issue of short date intervals as stated in the question above.
Upvotes: 0
Views: 708
Reputation: 25991
Elaborating the idea I had in the comments, if you choose an epoch date, we just count the number of days each dates are relative to the epoch. Then, we just note the day difference. This should iron our any issues you have with hours, minutes and seconds. Choose an appropriate epoch date to cancel out local time zone issues and day light savings issues:
function countDays(srcDate, dstDate) {
let epoch = new Date("2000-01-01 00:00:00")
let srcDays = Math.floor((srcDate.getTime() - epoch.getTime()) / 1000 / 60 / 60 / 24)
let dstDays = Math.floor((dstDate.getTime() - epoch.getTime()) / 1000 / 60 / 60 / 24)
let count = dstDays - srcDays
console.log("srcDate: ", srcDate)
console.log("dstDate: ", dstDate)
console.log("count: ", count)
return count
}
countDays(new Date("2019-01-26 23:59:59"), new Date("2019-01-27 00:00:00"))
countDays(new Date("2019-01-26 11:59:59"), new Date("2019-01-27 00:00:00"))
countDays(new Date(1561978969653), new Date())
Upvotes: 1
Reputation: 1937
I'd suggest using the setHours method to set the hours, minutes, seconds and milliseconds to zero (giving you the start of the relevant day) for both the start and end date objects, and then calculate the difference between those.
Conveniently, the setHours method returns the number of milliseconds since Jan 1970, the same as the getTime method, so you can get rid of those calls as well.
function diff(a,b) {
let timeDiff = Math.abs(a.setHours(0,0,0,0) - b.setHours(0,0,0,0));
let days = Math.round(timeDiff / (1000*3600*24));
console.log(a + " -> " + b + ": " + days + " days");
return days;
}
diff(new Date("Jun 01, 2019 23:59:00"), new Date("Jun 02, 2019 00:01:00"));
Upvotes: 1
Reputation: 3408
Use Math.round on the absolute value to get the next full value of the day.
function calculateDaysBetween(now, uploadDate) {
let timeDiff = Math.abs(now.getTime() - uploadDate.getTime());
return Math.round(Math.abs((timeDiff)/((24*60*60*1000))));
}
let now = new Date("Jan 27, 2019 00:00:00");
let uploadDate = new Date("Jan 26, 2019 11:59:59");
console.log(calculateDaysBetween(now, uploadDate)); // 1 Day
let now2 = new Date("Jun 01, 2019 00:00:00");
let now3 = new Date("Jun 01, 2019 00:01:00");
console.log(calculateDaysBetween(now2, now3)); // 0 Days
let now4 = new Date("Jun 10, 2019 10:00:59");
let now5 = new Date("Jun 15, 2019 11:59:00");
console.log(calculateDaysBetween(now4, now5)); // 5 Days
Upvotes: 1