How to get the different between 2 dates in hours using javascript

The difference can be done substracting the 2 dates.

Syntax:

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

Example:

This example creates 2 date variables, the first one at at 1 am and the second one at 5 am. The 2 dates are substracted to get the time difference in milli seconds. It is then divided by 36e5 to get the time difference in hours. The output is returned using an alert.

var date1 = new Date("July 21, 2000 01:15:00");
var date2 = new Date("July 21, 2000 05:15:00");
alert( Math.abs(date2 - date1) / 36e5);


References:

jQuery

Recent Comments