0

Sorry if the question is a duplicate but I couldn't find anything on my case here. I got a function getDaysDiff where I pass two arguments (startDate, endDate) which are date strings, and expect to get the difference between two dates in days but with some details. Like so:

  1. If I pass '2021-07-19T23:59:59' and '2021-07-20T00:00:01' I want to get 1 day as a return value because the dates are different even though the number of hours is less than 24
  2. Same for the case where the two dates are close but the number of hours is more than 24, like '2021-07-19T00:00:01' and '2021-07-20T23:59:59', in this case, I still want to get 1 day
  3. And finally it should work as well with the adjacent dates from different months, like '2021-07-30T23:59:59' and '2021-08-01T00:00:01' should return 1 day as well.

I tried to apply the following function but thanks to Math.round/floor/ceil it breaks anyways for one of the cases above.

function getDaysDiff(start, end) {
  const date1 = new Date(start);
  const date2 = new Date(end);

  const oneDay = 1000 * 60 * 60 * 24;

  const diffInTime = date2.getTime() - date1.getTime();

  const diffInDays = Math.ceil(diffInTime / oneDay);

  return diffInDays;
}

console.log(getDaysDiff('2021-07-19T23:59:59', '2021-07-20T00:00:01'));
console.log(getDaysDiff('2021-07-19T00:00:01', '2021-07-20T23:59:59'));
console.log(getDaysDiff('2021-07-31T23:59:59', '2021-08-01T00:00:01'));

Is there a consice way to get this difference?

Slava.In
  • 597
  • 1
  • 9
  • 22
  • 2
    In other words, you want the day of month difference, regardless of the time? –  Jul 20 '21 at 09:40
  • 1
    Dont work with time. Just work with the date right? – Emile Jul 20 '21 at 09:41
  • That's correct @ChrisG – Slava.In Jul 20 '21 at 09:42
  • using getDate() instead of getTime() fails on the last condition, wrapping month. – Emile Jul 20 '21 at 09:46
  • 1
    Before subtracting, call `setHours(0)` on start and end, same for minutes and seconds: https://jsfiddle.net/tLfpv50b/ –  Jul 20 '21 at 09:46
  • 1
    Why is the expected difference between `'2021-07-30T23:59:59'` and `'2021-08-01T00:00:01'` one day? That are not adjacent dates –  Jul 20 '21 at 09:48
  • 1
    @jabaa 2, because July has actually 31 days. Probably just a mistake by OP. –  Jul 20 '21 at 09:48
  • @jabaa , sorry a typo – Slava.In Jul 20 '21 at 09:50
  • lol, throwing that use case. – Emile Jul 20 '21 at 09:51
  • @chrisG you should post the answer. i can confirm date1.setHours(0,0,0) date2.setHours(0,0,0) works. – Emile Jul 20 '21 at 09:52
  • @ChrisG, that works great, post it as an answer please – Slava.In Jul 20 '21 at 09:53
  • 2
    I would, it's a dupe though: [Comparing date part only without comparing time in JavaScript](https://stackoverflow.com/questions/2698725/comparing-date-part-only-without-comparing-time-in-javascript) –  Jul 20 '21 at 09:56
  • 1
    Given the format of the timestamps, you can do: `let daysDiff = Math.round((new Date(dateA).setHours(0,0,0,0) - new Date(dateB).setHours(0,0,0,0)) / 8.64e7)`. Rounding is required as if the dates go over a daylight saving boundary, the result will not be an exact multiple of ms in a day. – RobG Jul 21 '21 at 10:11

0 Answers0