create an array of dates between two dates
Hello,
I am having an issue with a section of my code which I have tried nearly EVERYTHING I can think of to correct. I have a form which is being used as a Vacation request form. On it I have two date pickers which matter:
Start Date --> Date_5
End Date--> Date_6
I am trying to calculate the difference in days between these two dates factoring out Holidays (which are stored in an array) & working weekends (also stored in an array) and Weekends (which I have set in an Array of two values "SAT" & "SUN"). For right now my employer does not have a need for working weekends but it has been discussed (time in lieu etc) so it was important that it is there as an option. When I get my start date and end dates captured (code block 1 below) and have them run through the function (getDateArray) something VERY odd happens. The formatting I want is perfect BUT the start date gets bumped "up" one day and thus all of the calculations are wrong.
EX:
I select May 27, 2019 as my start date. I then select May 27, 2019 as my end date. My expectation is that I'd end up with an array of one element (Mon May 27 2019) instead I end up with an Array with Sun May 26 2019. This date then gets "removed" as a weekend and then I get a date count of 0.
Code Block 1: (captures and stores my fields into variables)
var startDate = new Date(this.getField("Date_5").value); //captures Start date from the form
var endDate = new Date(this.getField("Date_6").value); //captures End date from the form
Code Block 2: (function to create date array)
var getDateArray = function(start, end) {
var arr = new Array();
var dt = new Date(start);
while (dt <= end) {
arr.push((new Date(dt)).toString().substring(0,15)); //save in format <DOW<<MMM><DD><YYYY>
dt.setDate(dt.getDate() + 1);
}
return arr;
}
I then found another option
var getDates = function(startDate, endDate) {
var dates = [],
currentDate = startDate,
addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
my issue is that even when I eventually find an option which provides the correct value for my variable I am storing my new date array and it does not save it in the correct format I NEED to perform the rest of my forms logic. I am looking for each date to be stored in the format <Day of Week - 3 Chars> <Month - 3 Chars> <Day - two Chars><Year - 4 Chars>. As I said code block 2 above DOES yield the correct "formatting" but for some reason I cannot figure out why it is adjusting my start date and adding a day to it.
I am certain that this is something I am missing here. As an FYI I have also tried the toLocalDateString() and defined an options variable....which I could not get working either.
If there is any help out there I'd appreciate it.
Rich
