• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Need to Calculate difference between 2 dates in YE...

# Need to Calculate difference between 2 dates in YEAR , MONTH & DAYS

Engaged ,
May 20, 2020 May 20, 2020

Copied

I need the difference between 2 dates  in three different field:

In first field :  it should in Years

2nd Field :  it should be Month,

3rd Field :  it should be Days

For ex-  First Date: 14/05/1998   second date: 18/04/2019

difference should show   1st Field:   20 years

2nd Field : 11 months

3rd Field :   04 days

I searched regarding this on this community and found few scripts, accordingly i converted the full difference in milliseconds.. and converted them to days, months, year.. everything works fine

but days are not calculated correctly due to 30,31, 28,29 days and in month due leap year.

Below are the scripts used after the difference is converted to milliseconds (TotalTime_MS)

for day field:

event.value = Math.floor((this.getField("TotalTime_MS").value)/(1000*60*60*24)%365.25);

For Month Field:

event.value = Math.floor( ((this.getField("TotalTime_MS").value)/(1000*60*60*24*30))%12 );

For Year Field:

event.value = Math.floor((this.getField("TotalTime_MS").value)/(1000*60*60*24*365.25));

TOPICS
Acrobat SDK and JavaScript

Views

2.4K

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 Expert , May 21, 2020 May 21, 2020

Sure, it's possible. You need to first convert the strings to Date objects, and then use that object's various functions to figure out the difference between the dates. All of the necessary functions are documented here: https://www.w3schools.com/jsref/jsref_obj_date.asp

4 Replies 4
Community Expert ,
May 20, 2020 May 20, 2020

Copied

This is not a math issue, but a definitions issue. You need to define what you mean by "years" and "months".

As you've seen, the length of both of those can vary, so you need to clearly define what should be the result for all possible combinations, and based on that write the logic for your calculation.

Report

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

Copied

Obviously YEAR mean CALENDAR YEAR  and same with MONTH means CALENDAR MONTH.

Is that not possible in JavaScript..... is year and month are not specified in Javascript.

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 Expert ,
May 21, 2020 May 21, 2020

Copied

Sure, it's possible. You need to first convert the strings to Date objects, and then use that object's various functions to figure out the difference between the dates. All of the necessary functions are documented here: https://www.w3schools.com/jsref/jsref_obj_date.asp

Report

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

Copied

LATEST

Thanks for your guidance.... At last I was able to do what actually  was  required..

Thanks again.