2 Replies Latest reply: Apr 3, 2013 10:29 PM by Doug

# Javascript anomoly on day count between two dates

Using ApEx 4.0, I have found an anomoly in some javascript code that calculates the number of days between two dates, the current_date and the past_date. If the past-date is on or before March 10, 2013, and the current_date is between March 10, 2013, and November 3, 2013, the day count will be 1 day less than the actual count. Between November 3, 2013, and November 4, 2013, the count increments by 2, and then the count will be accurate from this date forward.

Here are examples:

Mar 10, 2013 = 69 days from 31-DEC-2012
Mar 11, 2013 = 69 days from 31-DEC-2012
Mar 12, 2013 = 70 days from 31-DEC-2012

Nov. 3 2013 = 306 days from 31-DEC-2012
Nov. 4 2013 = 308 days from 31-DEC-2012

March 11 should be 70, and March 12 should be 71. November 3 should be 307, and November 4 corrects the wrong count which began March 11.

Changing the past_date to March 10, 2013 produces the following:

10-Mar-2013 = 0 days from 10-Mar-2013
11-Mar-2013 = 0 days from 10-Mar-2013
12-Mar-2013 = 1 days from 10-Mar-2013

But changing the past_date to March 11, 2013, produces correct numbers:

11-Mar-2013 = 0 days from 11-Mar-2013
12-Mar-2013 = 1 days from 11-Mar-2013
13-Mar-2013 = 2 days from 11-Mar-2013

I would certainly appreciate anyone's help on identifying the cause of this anomoly. Here is the javascript code:

var w1= (\$v("P48_PAST_DATE"));
w1 = (w1.toString());
var vmon = (w1.substr(3,3));
var vyr = (w1.substr(7));
var r = (vyr.length);
if (r == 2)
vyr = (parseFloat(vyr) + 2000);
var vday = (w1.substr(0,2));
var y = (vmon.concat(" ",vday,", ",vyr));
y = Date.parse(y);

var w2 = (\$v("P48_CURRENT_DATE"));
var vmon2 = (w2.substr(3,3));
var vyr2 = (w2.substr(7));
var vday2 = (w2.substr(0,2));
var x = (vmon2.concat(" ",vday2,", ",vyr2));
x = Date.parse(x);

var numdays = (x - y);
numdays = (Math.floor(numdays / 86400000));
\$s("P48_NUMBEROFDAYS",numdays);
• ###### 1. Re: Javascript anomoly on day count between two dates
Did you google for something like "javascript number of days between two dates". I think you will find the explanation in this observation:

This method doesn't work properly if there's a daylight savings jump between the two dates.

There are examples to be found to calculate the difference between two dates.
• ###### 2. Re: Javascript anomoly on day count between two dates
Thanks for the information.