Royi Namir
Royi Namir

Reputation: 148524

JavaScript - Calc days between 2 dates (Including end date)?

Ive been searching quite a lot find an answer for this , and couldn't find one.

I have 2 dates , I want to calc days between them

but I Also want the end day to be counted.

Example :

the only code which I find working is :

 Math.round((b - a) / ( 1000 * 60 * 60 * 24)) +1 

where :

var a= new Date (y,0,20);
var b= new Date (y,0,26);

I also made a nested loop to test all months within a 150 years , and it works Ok.

var y = 1970;
var m = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
for(var i = 0; i < 150; i++)
{
    for(var j = 0; j < 12; j++)
    {
        var a = new Date(y + i, m[j], 20);
        var b = new Date(y + i, m[j], 26);
        var days = Math.round((b - a) / (1000 * 60 * 60 * 24)) + 1;
        if(days != 7) console.log(i + y); //tell me if result is unexpected.
    }
}
console.log('-FINISH-' + new Date().getTime())

So where is the problem ?

I can't figure how the math function like round can do the trick here.

We are talking about milliseconds here and I can't see where the round behavior gives me the right result.

(p.s. - forget about ceil and floor , they do not help here and have inconsistent results) , I have also tried to add one day to the b value , and use ceil || floor but with no success

here is the code

Upvotes: 1

Views: 665

Answers (4)

Marc
Marc

Reputation: 129

As already answered in a previous post:

var days = Math.floor(enddate.getTime()-startdate.getTime())/(24*60*60*1000);

Upvotes: -1

David Hedlund
David Hedlund

Reputation: 129792

The reason your rounding is working is because you're only working with full days. myDate.getTime() will yield the number of milliseconds since 1970-01-01. If you're always assigning your dates as new Date(y,m,d) you will always have the time part set to 00:00:00.000, and hence the date comparison will always yield a multiple of 86400000, which is your divisor. The rounding here is for the most part superfluous.

If you're creating all of your dates as specified above, the only time rounding does come into play, is when the daylight savings offset at date b is different from that at date a. round will take care of these discrepancies, as they're rarely more than an hour.

From your script, October 1970 is problematic (in CEST) because Oct 20th is in daylight savings, and Oct 26th isn't.

var a = new Date(1970, 9, 20);
var b = new Date(1970, 9, 26);

(b - a) / (1000 * 60 * 60 * 24) // 6.041666666666667

You could work around this by rounding, or by using UTC dates

var a = new Date(Date.UTC(1970, 9, 20));
var b = new Date(Date.UTC(1970, 9, 26));

(b - a) / (1000 * 60 * 60 * 24) // 6

Upvotes: 1

jantimon
jantimon

Reputation: 38142

I found the reason! :) (e.g. year 2035)

Date {Sun Mar 25 2035 00:00:00 GMT+0100 (CET)} 
Date {Mon Mar 26 2035 00:00:00 GMT+0200 (CEST)}

Look at the GMT Times one is GMT+0100 (CET) and one is GMT+0200 (CEST)

To get always the result without round you have to use UTC:

var a = Date.UTC(y + i, m[j], 25);
var b = Date.UTC(y + i, m[j], 26);
var days = (b - a) / (1000 * 60 * 60 * 24);

I modified your code slightly to find this bug:

var y = 1970;
var m = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
for(var i = 0; i < 150; i++)
{
    for(var j = 0; j < 12; j++)
    {
        var a = new Date(y + i, m[j], 25);
        var b = new Date(y + i, m[j], 26);
        var days = (b - a);
        if(days != 86400000) console.log(i + y, days); //tell me if result is unexpected.
    }
}


1983 90000000
1984 82800000
1988 90000000
...

This was already discussed here:

http://www.webdeveloper.com/forum/archive/index.php/t-5195.html

Upvotes: 1

bezmax
bezmax

Reputation: 26122

Here is your answer:

http://www.direct.gov.uk/en/Nl1/Newsroom/DG_185398

Basically, March 25 has 23 hours in it instead of 24. And 28'th October has 25 hours in it. That's why rounding works well while floor/ceil does not.

Also, as you can see, DST date changes with each year (you can see that in same article), that's why only once every 5-10 years the floor/cail test fails. On other years DST is applied to different date, therefore test result appears to be normal.

Upvotes: 1

Related Questions