Background information
I am setting up a function which creates a date array based on a start date and an end date.
The function will receive start and end dates, which have first been formatted to year-month-dayT12:00:00:00
format and then converted into milliseconds with .getTime()
format.
My script
I have made the following script to create the array.
var $date_array = [];
function calc_workdays_between_dates (a, b) {
function $create_date_array ($start_date, $end_date) {
var $counter = 0;
while ($start_date !== $end_date) {
var x = new Date($start_date);
x.setDate(x.getDate() + $counter);
$date_array.push(x);
$start_date = x.getTime();
$counter++;
}
}
$create_date_array (a, b);
}
Please be aware that there is a reason for nesting the $create_date_array
function inside the $calc_workdays_between_dates
function. For now I have stripped out all other parts of the $calc_workdays_between_dates
function to focus solely on the problem at hand (I am also running my tests on this stripped down version - so the rest of the function is not there to affect anything).
My problem
Example 1:
If I invoke the function with calc_workdays_between_dates (x1, x2);
where:
x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function
x2 = new Date("2015-04-07")
it results in $date_array
getting the following content:
Sat Apr 04 2015 12:00:00 GMT+0200 (CEST)
Sun Apr 05 2015 12:00:00 GMT+0200 (CEST)
Tue Apr 07 2015 12:00:00 GMT+0200 (CEST)
As you can see the function for some reason skips monday (one day in total).
Example 2:
x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function
x2 = new Date("2015-04-10")
results in:
Sat Apr 04 2015 12:00:00 GMT+0200 (CEST)
Sun Apr 05 2015 12:00:00 GMT+0200 (CEST)
Tue Apr 07 2015 12:00:00 GMT+0200 (CEST)
Fri Apr 10 2015 12:00:00 GMT+0200 (CEST)
As you can see the function somehow skips Monday, Wednesday and Thursday (3 days in total).
Example 3:
x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function
x2 = new Date("2015-04-14")
results in:
Sat Apr 04 2015 12:00:00 GMT+0200 (CEST)
Sun Apr 05 2015 12:00:00 GMT+0200 (CEST)
Tue Apr 07 2015 12:00:00 GMT+0200 (CEST)
Fri Apr 10 2015 12:00:00 GMT+0200 (CEST)
Tue Apr 14 2015 12:00:00 GMT+0200 (CEST)
As you can see the function in thise instance skips Monday, Wednesday, Thursday, Saturday, Sunday and Monday (6 days in total).
Example 4:
x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function
x2 = new Date("2015-04-08")
results in the function not working. It appears that the while loop continues to run endlessly.
My question
What is making the script skip days?
You calculate the next date based on $start_date
and counter
. However, in the while-loop $start_date
is reassigned and thus not represent the start date anymore. Therefore it should not be incremented with counter
, but only with one.
A correct solution would be:
while ($start_date !== $end_date) {
var x = new Date($start_date);
x.setDate(x.getDate() + 1);
$date_array.push(x);
$start_date = x.getTime();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With