Although the loop counter grows "exponentially" despite using ++

Background Information

I create a function that creates an array of dates based on the start date and end date.

The function will receive start and end dates, which were first formatted in the format year-month-dayT12:00:00:00 , and then converted to milliseconds with the format .getTime() .

My script

I created the following script to create an 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); } 

Remember that there is a reason for nesting the $create_date_array function inside the $calc_workdays_between_dates function. At the moment, I have removed all other parts of the $calc_workdays_between_dates function to focus exclusively on the problem (I also run my tests on this stripped-down version), so the rest of the function does not affect anything).

My problem

Example 1:

If I call a 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") 

this causes $date_array receive 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, for some reason the function skips Monday (just one day).

Example 2:

 x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function x2 = new Date("2015-04-10") 

leads to:

 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 (only 3 days).

Example 3:

 x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function x2 = new Date("2015-04-14") 

leads to:

 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 this instance skips Monday, Wednesday, Thursday, Saturday, Sunday, and Monday (only 6 days).

Example 4:

 x1 = new Date("2015-04-04") //formatted and converted to ms before invoking function x2 = new Date("2015-04-08") 

causes the function to not work. The while loop seems to continue to run indefinitely.

My question

What does a script skip days?

+6
source share
1 answer

You calculate the next date based on $start_date and counter . However, in the while-loop, $start_date reassigned and therefore no longer represents the start date. Therefore, it cannot be increased with counter , but with only one.

Correct solution:

 while ($start_date !== $end_date) { var x = new Date($start_date); x.setDate(x.getDate() + 1); $date_array.push(x); $start_date = x.getTime(); } 
+7
source

Source: https://habr.com/ru/post/987661/


All Articles