45

I doing a function in Javascript like the VisualBasic DateDiff.

You give two dates and the returning time interval (Seconds, Minutes, Days, etc...)

DateDiff(ByVal Interval As Microsoft.VisualBasic.DateInterval, _
  ByVal Date1 As Date, ByVal Date2 As Date) as Long

So what's the best way to calculate the difference of Javascript Dates?

InfoStatus
  • 6,983
  • 9
  • 41
  • 53

2 Answers2

66

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.

Nathan Feger
  • 19,122
  • 11
  • 62
  • 71
Anand
  • 7,654
  • 9
  • 46
  • 60
  • 31
    You don't need to use `getTime`. If you just do `return date1 - date2;`, the result is the same. (And that's not an implementation-specific thing, it's in the spec, although somewhat indirectly). – T.J. Crowder May 27 '10 at 13:00
  • 3
    I would recommend using `return date2.getTime() - date1.getTime();` so the order of the dates is as expected (otherwise a negative number will be returned). – kingjeffrey Aug 10 '11 at 19:19
  • 1
    This answer is accepted, but is wrong, unfortunately. Try the code `new Date(new Date()-new Date());` – P Varga Dec 03 '11 at 02:32
  • 2
    @PéterVarga Why are you wrapping it in another `new Date()`? In the case you describe the result the OP is interested in is `0`, not `new Date(0);` – alnorth29 Sep 24 '12 at 22:19
  • 2
    `Shouldn't be too difficult`. Hm, really? Months/years counting is difficult(if even possible) we should to use `when` and `where` information for count dst, leap years etc. – vp_arth Jun 28 '15 at 17:40
  • @T.J.Crowder and other commenters: Using getTime() for Date differences is syntactically correct while directly subtracting Date objects is not. Browsers still accept that because of backward compatibility. But all modern JS linters will throw at you. – Jpsy Jan 21 '23 at 08:42
  • @Jpsy - There's nothing that's not "syntactically correct" about `a - b` when `a` and `b` are `Date` objects, and support for it is not just for backward compatibility. This is all very clearly defined in the JS spec. Most of JS's operators do automatic value conversion, incl. the subtraction operator. The value conversion from `Date` to `number` is clearly defined and not deprecated. As for linters, well, sure, linters are opinionated (often in good ways). I've never seen a linter complain about date subtraction (ESLint doesn't, not even with `no-implicit-coercion`), but sure, one might. – T.J. Crowder Jan 21 '23 at 11:57
6

If you follow this tutorial, one way is to use:

Date.getTime()

You will find a full javascript function here, complete with date validation.

That being said, as commented by Rafi B. 5 years later, "Get difference between 2 dates in javascript?" is more precise.

var _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
  // Discard the time and time-zone information.
  var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

  return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It is giving wrong hour, If you put 2PM to 11.59PM on the same day. Instead of giving 10 hour it is giving 10:59. Can you you check it out? – jewelhuq Dec 25 '16 at 00:52
  • @jewelhuq 8 years later, I would need a bit more details. Can you ask a separate question? – VonC Dec 25 '16 at 00:52
  • var start = new Date("2016-12-24 14:00"); var end = new Date("2016-12-24 23:59"); var diffMs = (end.getTime() - start.getTime()); // milliseconds between now & Christmas var diffDays = Math.round(diffMs / 86400000); // days var diffHrs = Math.round((diffMs % 86400000) / 3600000); // hours ar diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes console.log(diffHrs); it is returning 10:59hour, It works fine till 23:00 . whats the problem going on. – jewelhuq Dec 25 '16 at 00:57
  • @jewelhuq Note: the link javascript.internet.com was no longer valid. I have restored it. I have also pointed to a more accurate solution. Finally, regarding your last comment, that is best addressed in a new question. – VonC Dec 25 '16 at 00:58
  • which is the more accurate solution ? – jewelhuq Dec 25 '16 at 01:04
  • @jewelhuq the one I describe in the edited answer as being more precise: http://stackoverflow.com/a/15289883/6309 – VonC Dec 25 '16 at 01:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131429/discussion-between-jewelhuq-and-vonc). – jewelhuq Dec 25 '16 at 01:07