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

Calculate time difference

Explorer ,
Aug 28, 2023 Aug 28, 2023

I have two date & time fields 

28/08/2023 09:00

30/08/2023 11:00 

 

and I have another field where I want the field to show as "Okay" when the difference between the two times is within 48h, and "Expired" when the difference is greater than 48h.

 

Would someone be able to help please. Thanks

TOPICS
JavaScript , PDF , PDF forms
550
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 ,
Aug 28, 2023 Aug 28, 2023

Let's say your fields are named "Date1" and "Date2" as custom calculation script' of third field, use this:

var date1 = this.getField("Date1").valueAsString;
var date2 = this.getField("Date2").valueAsString;
var d1 = util.scand("dd/mm/yyyy HH:MM", date1);
var d2 = util.scand("dd/mm/yyyy HH:MM", date2);

if(date1 === "" || date2 === "")
 event.value = ""; 
else{
 var timeDifference = d2 - d1;
 if (timeDifference >= 0 && timeDifference <= 172800000)
  event.value = "Okay";
 else
  event.value = "Expired";}

You can change condition to allow minor variations if needed.

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 ,
Aug 29, 2023 Aug 29, 2023
LATEST

If you're using milliseconds to compare times you have to take into account the hour change because of the daylight saving time, which adds or removes one hour twice per year. It's better to compare full dates, if possible.

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