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

I need the number of days between two dates in PDF form

New Here ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

Have attempted to generate the number of days between two dates using a custom calculation in a text field form and no matter what formula I use the field blank.

 

Can anyone help?

TOPICS
Create PDFs , JavaScript , PDF , PDF forms

Views

203

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 2 Correct answers

Community Expert , Sep 11, 2024 Sep 11, 2024

I assume you have two dates fields and want to show number of days in 3rd field, let's say two dates fields are named "Date1" and "Date2", use this as custom calculation script of 3rd field:

 

var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;

if (date1 && date2) {
 var d1 = new Date(date1);
 var d2 = new Date(date2);
 var diffInMs = d2.getTime() - d1.getTime();

 var diffInDays = diffInMs / (1000 * 60 * 60 * 24);
  event.value = diffInDays.toFix
...

Votes

Translate

Translate
Community Expert , Sep 11, 2024 Sep 11, 2024

Here you go:

var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;

if (date1 && date2) {
 var d1 = new Date(date1);
 var d2 = new Date(date2);

 var year1 = d1.getFullYear();
 var year2 = d2.getFullYear();
 var month1 = d1.getMonth();
 var month2 = d2.getMonth();

 var diffInMonths = (year2 - year1) * 12 + (month2 - month1);

 event.value = diffInMonths;} 
else {
 event.value = "";}

Votes

Translate

Translate
Community Expert ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

I assume you have two dates fields and want to show number of days in 3rd field, let's say two dates fields are named "Date1" and "Date2", use this as custom calculation script of 3rd field:

 

var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;

if (date1 && date2) {
 var d1 = new Date(date1);
 var d2 = new Date(date2);
 var diffInMs = d2.getTime() - d1.getTime();

 var diffInDays = diffInMs / (1000 * 60 * 60 * 24);
  event.value = diffInDays.toFixed(0);} 
else {
 event.value = "";}

 

 

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
New Here ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

Perfect it works! 

Is there a Java script to count months between two dates.

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 ,
Sep 11, 2024 Sep 11, 2024

Copy link to clipboard

Copied

Here you go:

var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;

if (date1 && date2) {
 var d1 = new Date(date1);
 var d2 = new Date(date2);

 var year1 = d1.getFullYear();
 var year2 = d2.getFullYear();
 var month1 = d1.getMonth();
 var month2 = d2.getMonth();

 var diffInMonths = (year2 - year1) * 12 + (month2 - month1);

 event.value = diffInMonths;} 
else {
 event.value = "";}

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
New Here ,
Sep 12, 2024 Sep 12, 2024

Copy link to clipboard

Copied

LATEST

Thank you so 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