convert year YYYY to year YY

Explorer ,
Dec 10, 2018

Copy link to clipboard

Copied

In the script at lines 1 thru 7, this.getField("date.Closing.Actual.Date") has a date format of mm/dd/yy and its value determines the yyyy value of this.getField("date.YrOfClosing") which has a yyyy custom date format.  this.getField("date.Prior1231") has a date format of mm/dd/yy . I need to use the yyyy value to set the yy value of this.getField("date.Prior1231") to 12/31/yy of the year before yyyy (where mm/dd will always be 12/31). Ex: if the source field value is == 2018 then the value of this.getField("date.Prior1231") should be 12/31/17.

1. var actual = this.getField("date.Closing.Actual.Date").value;   //mm/dd/yy format

2. var d = util.scand("mm/dd/yy", actual);

3. var yearOfClosing = this.getField("date.YrOfClosing");   //YYYY format.

4.      yearOfClosing.value = d.getFullYear( );  //extracts year in YYYY format

5. var f = d.getFullYear().toString().substr(-2); //Ex: change 2018 to 18.

6. var priorDec31 = this.getField("date.Prior1231");

7.     priorDec31.value = new Date("December 31, " + (f - 1));

This throws this error: Invalid date/time: please ensure that the date/time exists. Field [ date.Prior1231 ] should match format mm/dd/yy

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

The value assigned to field "priorDec31" is not formatted.   change line 7 to this.

priorDec31.value = util.printd("mm/dd/yy",new Date("December 31, " + (f - 1)));

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

323

Likes

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

convert year YYYY to year YY

Explorer ,
Dec 10, 2018

Copy link to clipboard

Copied

In the script at lines 1 thru 7, this.getField("date.Closing.Actual.Date") has a date format of mm/dd/yy and its value determines the yyyy value of this.getField("date.YrOfClosing") which has a yyyy custom date format.  this.getField("date.Prior1231") has a date format of mm/dd/yy . I need to use the yyyy value to set the yy value of this.getField("date.Prior1231") to 12/31/yy of the year before yyyy (where mm/dd will always be 12/31). Ex: if the source field value is == 2018 then the value of this.getField("date.Prior1231") should be 12/31/17.

1. var actual = this.getField("date.Closing.Actual.Date").value;   //mm/dd/yy format

2. var d = util.scand("mm/dd/yy", actual);

3. var yearOfClosing = this.getField("date.YrOfClosing");   //YYYY format.

4.      yearOfClosing.value = d.getFullYear( );  //extracts year in YYYY format

5. var f = d.getFullYear().toString().substr(-2); //Ex: change 2018 to 18.

6. var priorDec31 = this.getField("date.Prior1231");

7.     priorDec31.value = new Date("December 31, " + (f - 1));

This throws this error: Invalid date/time: please ensure that the date/time exists. Field [ date.Prior1231 ] should match format mm/dd/yy

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

The value assigned to field "priorDec31" is not formatted.   change line 7 to this.

priorDec31.value = util.printd("mm/dd/yy",new Date("December 31, " + (f - 1)));

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

324

Likes

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
Dec 10, 2018 0
Adobe Community Professional ,
Dec 11, 2018

Copy link to clipboard

Copied

The value assigned to field "priorDec31" is not formatted.   change line 7 to this.

priorDec31.value = util.printd("mm/dd/yy",new Date("December 31, " + (f - 1)));

Likes

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
Reply
Loading...
Dec 11, 2018 1
Most Valuable Participant ,
Dec 11, 2018

Copy link to clipboard

Copied

You can't define a Date object with a two-digit year, which is why the last line in your code fails.

Run this code from the console:

new Date("December 31, 18");

And compare it to:

new Date("December 31, 2018");

Likes

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
Reply
Loading...
Dec 11, 2018 0
Explorer ,
Dec 11, 2018

Copy link to clipboard

Copied

Since the date format of this.getField("date.Prior1231") is mm/dd/yy, Thom Parker's solution needed to be modified by changing "December 31," to "12/31/" as follows, after which it works just as I needed: 

     priorDec31.value = util.printd("mm/dd/yy", new Date("12/31/" + (f - 1)));

Thanks very much.

Likes

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
Reply
Loading...
Dec 11, 2018 0
Most Valuable Participant ,
Dec 11, 2018

Copy link to clipboard

Copied

You're getting the wrong results, though. This code:

new Date("12/31/18")

Returns:

Tue Dec 31 1918 00:00:00 GMT+0100 (Romance Standard Time)

You're in effect inserting the Y2K-bug into your own code, for no good reason.

Likes

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
Reply
Loading...
Dec 11, 2018 0
Explorer ,
Dec 11, 2018

Copy link to clipboard

Copied

The 12/31/yy date is used to calculate the number of days between 12/31/yy and the target date in the following year (i.e. , which is also in the mm/dd/yy format). Using the yy format for both start and stop dates, my script is correctly calculating the number of days, however I'm guessing that is because the target date has the same defect, i.e. a target date of 2/15/19 returns Feb 15 1919 00:00:00 GMT+0100.

So the bottom line is that I should change the format of my date fields to yyyy, correct?

Likes

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
Reply
Loading...
Dec 11, 2018 0
Explorer ,
Dec 11, 2018

Copy link to clipboard

Copied

Here is another solution to my original question.

1. var actual = this.getField("date.Closing.Actual.Date").value;   //mm/dd/yy format

2. var d = util.scand("mm/dd/yy", actual);

3. var g = new Date(d.getFullYear(), 0, 0);// the 0, 0 parameters are the equivalent to the day before Jan. 1 of var actual

4. priorDec31.value = util.printd("mm/dd/yy", g);

As a bonus this solution eliminates my need for the yyyy formated field this.getField("date.YrOfClosing") that is included in my original post above. CAUTION: based on the guidance of MVP above it is apparent that the yy format of my fields should be changed to yyyy.

Likes

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
Reply
Loading...
Dec 11, 2018 0
Most Valuable Participant ,
Dec 11, 2018

Copy link to clipboard

Copied

Yes, use four digits for the year.

Likes

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
Reply
Loading...
Dec 11, 2018 0
Explorer ,
Dec 11, 2018

Copy link to clipboard

Copied

I understand that using a four digit year is preferable and will do that. However, since my scripts were returning accurate results I wanted to test what MVP said, which was this:

     new Date("12/31/18")

     Returns:

     Tue Dec 31 1918 00:00:00 GMT+0100 (Romance Standard Time)

but I did not get that result.

I entered the date 12/31/18 in an Acrobat date field with the mm/dd/yy format. Here is the script I used:

     var prior = this.getField("date.Prior1231").value;

     var gmt1231 = this.getField("txt.GMT.Dec31");

     var d = util.scand("mm/dd/yy", prior);

       gmt1231.value = new Date(d);

Using the mm/dd/yy format my script returns:

     Tue Dec 31 2018 00:00:00 GMT-0600 (CST)

Likes

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
Reply
Loading...
Dec 11, 2018 0
Most Valuable Participant ,
Dec 12, 2018

Copy link to clipboard

Copied

That's because what's creating the Date object in your case is the scand method, not the Date object constructor.

The scand method behaves differently (which is yet another reason not to use an ambiguous value like a two-digit year code).
Change your year value to "50" and see what happens...

Likes

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
Reply
Loading...
Dec 12, 2018 0
Most Valuable Participant ,
Dec 12, 2018

Copy link to clipboard

Copied

Bear in mind that 2000 is a leap year but but "00" (your chosen abbreviation, which means 1900) is not. This means date differences may not be accurate, if any of your dates go back before 1 March 2000.

Likes

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
Reply
Loading...
Dec 12, 2018 0
ODuinn LATEST
Explorer ,
Dec 13, 2018

Copy link to clipboard

Copied

Good to understand these things. Thank you.

Likes

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
Reply
Loading...
Dec 13, 2018 0