131

I got two Date objects and I want to calculate the difference in hours.

If the difference in hours is less than 18 hours, I want to push the date object into an array.

Javascript / jQuery, doesn't really matter; what works the best will do.

Anoniem Anoniem
  • 1,915
  • 6
  • 18
  • 19

3 Answers3

310

The simplest way would be to directly subtract the date objects from one another.

For example:

var hours = Math.abs(date1 - date2) / 36e5;

The subtraction returns the difference between the two dates in milliseconds. 36e5 is the scientific notation for 60*60*1000, dividing by which converts the milliseconds difference into hours.

Boaz
  • 19,892
  • 8
  • 62
  • 70
  • 62
    The engineer in me would prefer to see `3.6e6` ([normalized scientific notation](http://en.wikipedia.org/wiki/Scientific_notation)) instead of `36e5`. The book reader, however, would prefer `360000`. – Blazemonger Oct 07 '13 at 12:58
  • 21
    To each his own. – Boaz Oct 07 '13 at 13:00
  • 13
    @Blazemonger 360 000? I would prefer 3 600 000 ;) – Pphoenix Sep 10 '15 at 13:18
  • 9
    Good approach, although I get 2.0001191666666664 due to large number handling inaccuracies. I suggest supplying the result to Math.floor like `var hours = Math.floor(Math.abs(date1 - date2) / 36e5);` – David Salamon Sep 26 '16 at 14:13
  • 11
    To make code more readable to the average developer, I would stick with 60*60*1000 (or use 3600000 with a comment). 36e5 or 3.6e6 assumes that the developer knows scientific notation, which is not always the case. – thdoan Feb 07 '19 at 20:39
  • This seems to only take hours and not the difference in days in that calculation. – Jayson Sep 26 '19 at 15:50
  • @Jayson I’m not sure I understand. The question is indeed about the difference in hours. If you need the difference in days, divide the result by 24. – Boaz Sep 26 '19 at 15:57
  • @Boaz - correct. Maybe I misunderstood the question. But let's say I have a date object of 9/27/2019 00:00:00 and another date object of 9/29/2019 01:00:00 I seem to get the answer of 1hr, when it should in fact be 49hrs. I was able to find a fix though with the .getTime() function: 'function diff_hours(dt2, dt1) ' { ' ' var diff =(dt2.getTime() - dt1.getTime()) / 1000; ' diff /= (60 * 60); ' return Math.abs(Math.round(diff)); ' ' } – Jayson Sep 27 '19 at 16:12
  • @Jayson `Math.abs(new Date('09/27/2019 00:00:00')-new Date('09/29/2019 01:00:00'))/36e5` returns `49` as expected. Is this the calculation you're making? – Boaz Sep 27 '19 at 23:08
  • @Boaz - I think the issue I was having was because I was using input from a date and time picker which was a string return and not a date object. I imagine I could have converted them to a date object but using .getTime() seemed to work just as well. Plus I can use the string in other locations as vars. – Jayson Sep 29 '19 at 20:32
  • with this method, 2 date with 1 day difference give me `440982` hours – Dimitri Kopriwa Apr 22 '20 at 09:21
  • @DimitriKopriwa The arithmetic operation casts the dates to their UNIX timestamp values (e.g. `.getTime()` or `.valueOf()`), before subtraction takes place. It could be the dates are really that far apart, or the specific calculation is not actually returning hours. Please share the timestamps in question. – Boaz Apr 22 '20 at 09:28
  • `2020-04-22T06:00:00Z` and `2020-04-23T06:00:00Z` – Dimitri Kopriwa Apr 22 '20 at 09:34
  • 1
    @DimitriKopriwa `Math.abs((new Date('2020-04-22T06:00:00Z')-new Date('2020-04-23T06:00:00Z')))/36e5` seems to return `24`, as expected. [See demo here](https://jsfiddle.net/0Lez1duo/). – Boaz Apr 22 '20 at 09:41
  • @Blazemonger Any chance you could change your number to have 5 "0"s? I copied your value and got bugs because it should be `3600000` (3.6 million), not `360000` (0.36 million). – M - Aug 13 '21 at 03:16
  • 1
    @Blazemonger Personally I'd prefer `60*60*1000` so the origin of the resulting number is clear – Mike B Aug 19 '21 at 20:54
60

Try using getTime (mdn doc):

var diff = Math.abs(date1.getTime() - date2.getTime()) / 3600000;
if (diff < 18) { /* do something */ }

Using Math.abs() we don't know which date is the smallest. This code is probably more relevant:

var diff = (date1 - date2) / 3600000;
if (diff < 18) { array.push(date1); }
12

Use the timestamp you get by calling valueOf on the date object:

var diff = date2.valueOf() - date1.valueOf();
var diffInHours = diff/1000/60/60; // Convert milliseconds to hours
Matt Zeunert
  • 16,075
  • 6
  • 52
  • 78