⌛️ Automatically update durations based on changes.
1. Overview
This interaction calculates and sets the duration field when changes are made to the Start Date or `End Date fields.
2. Example
Below is an example of how it works:
- It retrieves the values of the
Start DateandEnd Datefields using agetValue()function. - It calculates the duration in milliseconds by subtracting the
Start Datevalue from theEnd Datevalue. It then converts the duration from milliseconds to days by dividing it by (1000 60 60 * 24) to obtain the number of days between the two dates. - The possible scenarios are:
| Scenarios | Actions |
|---|---|
If the calculated duration is negative (which can happen if the End Date is before the Start Date). | It sets the duration to 0 to avoid negative values. |
If the Start Date and End Date are the same. | It sets the duration to 1 day, ensuring that there's always a minimum duration of 1 day. |
If both the Start Date and End Date values are available. | It sets the calculated duration (in days) into the Duration field using setValue() function. |
startDate = May 2, 2023
endDate = May 3, 2023
duration = 1 days
async function handler(C) {
let actions = [];
let startDate = new Date(C.getValue("1662670-start-date")).getTime();
let endDate = new Date(C.getValue("1662670-end-date")).getTime();
let durationInMilliseconds = endDate - startDate;
// Convert milliseconds to days
let durationInDays = Math.ceil(durationInMilliseconds / (1000 * 60 * 60 * 24));
if (durationInDays < 0) {
durationInDays = 0;
}
// Set duration to 1 if startDate and endDate are the same
if (startDate === endDate) {
durationInDays = 1;
}
if (startDate && endDate) {
actions.push(C.setValue("1662670-duration", durationInDays));
}
return C.mergeAll(actions);
}📌 Need Help?
