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

convert year YYYY to year YY

Engaged ,
Dec 10, 2018 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

TOPICS
Acrobat SDK and JavaScript

Views

923

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 , Dec 11, 2018 Dec 11, 2018

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)));

Votes

Translate

Translate
Community Expert ,
Dec 11, 2018 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)));

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Dec 11, 2018 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");

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
Engaged ,
Dec 11, 2018 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.

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 ,
Dec 11, 2018 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.

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
Engaged ,
Dec 11, 2018 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?

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
Engaged ,
Dec 11, 2018 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.

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 ,
Dec 11, 2018 Dec 11, 2018

Copy link to clipboard

Copied

Yes, use four digits for the year.

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
Engaged ,
Dec 11, 2018 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)

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 ,
Dec 12, 2018 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...

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
LEGEND ,
Dec 12, 2018 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.

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
Engaged ,
Dec 13, 2018 Dec 13, 2018

Copy link to clipboard

Copied

LATEST

Good to understand these things. Thank you.

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