42

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,579
  • 9
  • 37
  • 52

2 Answers2

63

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
  • 18,596
  • 10
  • 58
  • 70
Anand
  • 7,552
  • 7
  • 42
  • 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());` – ᅙᄉᅙ 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
5

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,129,465
  • 480
  • 4,036
  • 4,755
  • 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