What’s the best way to calculate date difference in Javascript

Use the Date object like so:

function DateDiff(var /*Date*/ date1, var /*Date*/ date2) {
    return date1.getTime() - date2.getTime();
}

This will return the number of milliseconds difference between the two dates. Converting it to seconds, minutes, hours etc. shouldn’t be too difficult.

Leave a Comment