Search code examples
javascriptexceldatedatejs

Excel and javascript date calculations differ


I am trying to do a date calculation in javascript that matches one in excel.

//Excel 
=(EDATE(DATE(2000,6,1),60)-DATE(2012,1,20))

This gives a result of -2424

So I try to do the same calculation in javascript with the following

//Javascript -  using datejs library
a = Date.parse('2000/06/01').addMonths(60);
b = Date.parse('2012/01/20');
a.setHours(12,0,0);
b.setHours(12,0,0);
span = new TimeSpan(a - b);
console.log(span.getDays());

This gives a result of -2423

I can't see why i am losing a day. Can anyone shed light on how this could be happening.

Cheers for any help in advance. :)


Solution

  • If nothing else, two leapseconds occured in 2005 and 2008 (Both on Dec 31st), which either app could be taking into account. You've explicitly set your JS dates to "noon", but specified no times for Excel, so it's probably defaulting to midnight, which is when the leapseconds occur. That'd make your Excel date/times be 2 seconds off from the JS calcs, and could cross the midnight border, accounting for your one missing day.