Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.