Skip to main content
kenneth kam chuh21426993
Known Participant
August 29, 2023
Question

Calculate time difference

  • August 29, 2023
  • 1 reply
  • 704 views

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

This topic has been closed for replies.

1 reply

Nesa Nurani
Community Expert
Community Expert
August 29, 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.

try67
Community Expert
Community Expert
August 29, 2023

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.