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

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

New Here ,
Sep 11, 2024 Sep 11, 2024

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
862
Translate
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
2 ACCEPTED SOLUTIONS
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.toFixed(0);} 
else {
 event.value = "";}

 

 

View solution in original post

Translate
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

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 = "";}

View solution in original post

Translate
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

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 = "";}

 

 

Translate
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

Perfect it works! 

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

Translate
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

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 = "";}
Translate
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

Thank you so much!

Translate
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 ,
Jan 14, 2025 Jan 14, 2025

@Nesa Nurani  this is exactly what I need, but I also need it to add one day. I have tried multiple times but for some reason, even the original formula doesn't seem to be working, and I'm struggling to add the additional day. Can you please take a look at what I have and see if you can tell what I'm doing wrong?

var date1 = this.getField("Date5_af_date").valueAsString;
var date2 = this.getField("Date1_af_date ").valueAsString;

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

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

 

Thank you in advance. Any help is appreciated!

Translate
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 ,
Jan 14, 2025 Jan 14, 2025
LATEST

For start, you are not using variable:
if (Date5_af_date && Date1_af_date) {
var d1 = new Date(Date5_af_date);
var d2 = new Date(Date1_af_date);
Should be:

if (date1 && date2) {
var d1 = new Date(date1);
var d2 = new Date(date2);
Whitespace in Field Name: In Date1_af_date , there is an extra space at the end of the field name. This will cause an error since the field name won't match the actual field in your PDF.

Try this:

var date1 = this.getField("Date5_af_date").valueAsString;
var date2 = this.getField("Date1_af_date").valueAsString;

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

 if (!isNaN(d1) && !isNaN(d2)) {
  var diffInMs = d2.getTime() - d1.getTime() + (1000 * 60 * 60 * 24);
  var diffInDays = diffInMs / (1000 * 60 * 60 * 24);
   event.value = diffInDays.toFixed(0);} 
 else {
  event.value = "";}} 
else {
 event.value = "";}
Translate
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