How to get the hours difference between two date objects?
167,816
Solution 1
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.
Solution 2
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); }
Solution 3
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
Related videos on Youtube
Author by
Anoniem Anoniem
Updated on June 16, 2021Comments
-
Anoniem Anoniem almost 3 years
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.
-
Alex G over 9 yearsThis question wasn't answered in stackoverflow.com/questions/3224834/…. OP specifically says: GET HOURS (NOT DAYS).
-
Anderson Green about 2 yearsThis question is about the difference in hours between dates, so it shouldn't have been closed as a duplicate.
-
-
Blazemonger over 10 yearsThe engineer in me would prefer to see
3.6e6
(normalized scientific notation) instead of36e5
. The book reader, however, would prefer360000
. -
Boaz over 10 yearsTo each his own.
-
Pphoenix over 8 years@Blazemonger 360 000? I would prefer 3 600 000 ;)
-
David Salamon over 7 yearsGood 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);
-
thdoan over 5 yearsTo 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.
-
Jayson over 4 yearsThis seems to only take hours and not the difference in days in that calculation.
-
Boaz over 4 years@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.
-
Jayson over 4 years@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)); ' ' }
-
Boaz over 4 years@Jayson
Math.abs(new Date('09/27/2019 00:00:00')-new Date('09/29/2019 01:00:00'))/36e5
returns49
as expected. Is this the calculation you're making? -
Jayson over 4 years@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.
-
Dimitri Kopriwa about 4 yearswith this method, 2 date with 1 day difference give me
440982
hours -
Boaz about 4 years@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. -
Dimitri Kopriwa about 4 years
2020-04-22T06:00:00Z
and2020-04-23T06:00:00Z
-
Boaz about 4 years@DimitriKopriwa
Math.abs((new Date('2020-04-22T06:00:00Z')-new Date('2020-04-23T06:00:00Z')))/36e5
seems to return24
, as expected. See demo here. -
Marquizzo almost 3 years@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), not360000
(0.36 million). -
mblakesley almost 3 years@Blazemonger Personally I'd prefer
60*60*1000
so the origin of the resulting number is clear