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

Calculate the difference in years between two dates

Participant ,
Mar 09, 2023 Mar 09, 2023

Copy link to clipboard

Copied

Hi
I have 4 fields named year1, year2, year3, year4 respectively.
I want to calculate the difference in years with respect to the dates entered respectively in "HIRES1", "HIRES2", ASSUNCTION3, ASSUNCTION4.
In the calculation script of each field I have inserted this formula

var s1 = this.getField("HIRES1").value;date = util.scand("mm/dd/yyyy",s1);now = new Date();difference_in_years = (now.getFullYear()) - date. getFullYear();if (now.getMonth()<date.getMonth()) {difference_in_years--;} else if ((now.getMonth()==date.getMonth()) && (now.getDate()<date .getDate())) {difference_in_years--;}event.value = difference_in_years;

The formula works to calculate the years starting from the date inserted in "ASSUNCTION1", while for the other fields the console gives me this error message

TypeError: date is null
1:Console:Exec
undefined

Anyone who can help me solve this problem?
Thank you

TOPICS
JavaScript

Views

732

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 ,
Mar 09, 2023 Mar 09, 2023

Copy link to clipboard

Copied

Where does you use the field "ASSUNCTION1"?

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
Participant ,
Mar 09, 2023 Mar 09, 2023

Copy link to clipboard

Copied

sorry
the field is HIRES1 not ASSUNCTION1

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 ,
Mar 09, 2023 Mar 09, 2023

Copy link to clipboard

Copied

LATEST

First, when you post code, please format it so it can be read. 

The "util.scand" function returns null when it is given a date string that does not match the format provided. So, the date fields that are showing this error, must not be formatted correctly.  

Add a "console.println()" statement to the code to display the input date string.

 

 

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