• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

create an array of dates between two dates

Community Beginner ,
May 30, 2019 May 30, 2019

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

676

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 31, 2019 May 31, 2019

The issue is that the Date constructor only accepts date strings in a very specific format, and it's not the one you're using.
Instead, you should use the scand method of the util object, which is much more versatile. Try this:

var myDateFormat = "yyyy-mm-dd";

var startDate = util.scand(myDateFormat, this.getField("Date_5").valueAsString); //captures Start date from the form

var endDate = util.scand(myDateFormat, this.getField("Date_6").valueAsString); //captures End date from the form

And then to

...

Votes

Translate

Translate
Community Expert ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

What can you see in the console when you execute following code:

var startDate = new Date(this.getField("Date_5").value);

var endDate = new Date(this.getField("Date_6").value);

console.println(startDate);

console.println(endDate);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

Oh wow....OK so that is the last time I assume something....

in my form I have date_5 & date_6 both text fields with the following formatting:

General:

Name: Date_5

Form Field: visible

Orientation: 0 degrees

Appearance:

Border color & fill colour both set to none

text is helv.

Options:

Align left

all others unticked

Actions:

None

Format:

Select Format cat: Date

YYYY-MM-DD (is the format selected)

Validate:

None

Calculate:

None

I cannot believe it - the field itself is reading the date wrong....

Two Test Cases:

Scenario #1:

Dates selected as the same:

2019-06-03 (as entered on the form for date_5 & date_6)

Output in console:

Sun Jun 02 2019 20:00:00 GMT-0400 (Eastern Standard Time)

Sun Jun 02 2019 20:00:00 GMT-0400 (Eastern Standard Time)

Scenario #2:

Selected two dates left Jun 03 selected then selected June 5 as my end date

Output in Console:

Sun Jun 02 2019 20:00:00 GMT-0400 (Eastern Standard Time)

Tue Jun 04 2019 20:00:00 GMT-0400 (Eastern Standard Time)

First off thanks for taking the time to respond...second....how the heck is this even possible?

Now armed with information (again thanks) I can confirm that if I enter this code:

  1. var startDate = this.getField("Date_5").value; 
  2. var endDate = this.getField("Date_6").value; 
  3. console.println(startDate); 
  4. console.println(endDate);

 

Scenario #1:

Dates selected as the same:

2019-06-03 (as entered on the form for date_5 & date_6)

Output in console:

2019-06-03

2019-06-03

Scenario #2:

Selected two dates left Jun 03 selected then selected June 5 as my end date

Output in Console:

2019-06-03

2019-06-05

I then tried to take the startDate var and pass it through separately to a new variable and "convert" to the new date object and I yield the same results as my 1st set of scenarios....my dates in both variables are moved up by one day.

Any thoughts?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 31, 2019 May 31, 2019

Copy link to clipboard

Copied

LATEST

The issue is that the Date constructor only accepts date strings in a very specific format, and it's not the one you're using.
Instead, you should use the scand method of the util object, which is much more versatile. Try this:

var myDateFormat = "yyyy-mm-dd";

var startDate = util.scand(myDateFormat, this.getField("Date_5").valueAsString); //captures Start date from the form

var endDate = util.scand(myDateFormat, this.getField("Date_6").valueAsString); //captures End date from the form

And then to verify that the results are correct:

console.println(startDate);

console.println(endDate);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines