A very quick article to calculate the time between two timestamps and break it down into days, hours, minutes and seconds.
Why?
I've done this in lots of other systems but here's one in Zoho Deluge.
How?
We're going to make use of the .toLong() function applied to a datetime datatype variable which will return the Unix seconds.
// the now time v_NowTime = zoho.currenttime; // // correction: I want to specify a from datetime v_NowTime = '2021-09-21 21:27:15'; // // now specify a to datetime (we're doing a working day so we add 1 business day) v_NextTime = zoho.currentdate.addBusinessDay(1).toString("yyyy-MM-dd ") + "12:00:00"; // // convert to seconds v_NowEpoch = v_NowTime.toLong(); v_NextEpoch = v_NextTime.toTime().toLong(); // // calculate seconds in between v_UnixSeconds = v_NextEpoch - v_NowEpoch; // // determine days v_Days = floor(v_UnixSeconds / 1000 / 60 / 60 / 24); info v_Days; v_UnixSeconds = v_UnixSeconds - (v_Days * 1000 * 60 * 60 * 24); // // determine hours v_Hours = floor(v_UnixSeconds / 1000 / 60 / 60); info v_Hours; v_UnixSeconds = v_UnixSeconds - (v_Hours * 1000 * 60 * 60); // // determine minutes v_Minutes = floor(v_UnixSeconds / 1000 / 60); info v_Minutes; v_UnixSeconds = v_UnixSeconds - (v_Minutes * 1000 * 60); // // determine seconds remaining v_Seconds = floor(v_UnixSeconds / 1000); info v_Seconds; // v_DayGrammar = if(v_Days == 1, "DAY", "DAYS"); v_HourGrammar = if(v_Hours == 1, "HR", "HRS"); v_MinGrammar = if(v_Minutes == 1, "MIN", "MINS"); v_SecGrammar = if(v_Seconds == 1, "SEC", "SECS"); // info v_Days + " " + v_DayGrammar + ", " + v_Hours + " " + v_HourGrammar + ", " + v_Minutes + " " + v_MinGrammar + ", " + v_Seconds + " " + v_SecGrammar ; // yields: 0 DAYS, 23 HRS, 54 MINS, 11 SECS
- // the now time
- v_NowTime = zoho.currenttime;
- //
- // correction: I want to specify a from datetime
- v_NowTime = '2021-09-21 21:27:15';
- //
- // now specify a to datetime (we're doing a working day so we add 1 business day)
- v_NextTime = zoho.currentdate.addBusinessDay(1).toString("yyyy-MM-dd ") + "12:00:00";
- //
- // convert to seconds
- v_NowEpoch = v_NowTime.toLong();
- v_NextEpoch = v_NextTime.toTime().toLong();
- //
- // calculate seconds in between
- v_UnixSeconds = v_NextEpoch - v_NowEpoch;
- //
- // determine days
- v_Days = floor(v_UnixSeconds / 1000 / 60 / 60 / 24);
- info v_Days;
- v_UnixSeconds = v_UnixSeconds - (v_Days * 1000 * 60 * 60 * 24);
- //
- // determine hours
- v_Hours = floor(v_UnixSeconds / 1000 / 60 / 60);
- info v_Hours;
- v_UnixSeconds = v_UnixSeconds - (v_Hours * 1000 * 60 * 60);
- //
- // determine minutes
- v_Minutes = floor(v_UnixSeconds / 1000 / 60);
- info v_Minutes;
- v_UnixSeconds = v_UnixSeconds - (v_Minutes * 1000 * 60);
- //
- // determine seconds remaining
- v_Seconds = floor(v_UnixSeconds / 1000);
- info v_Seconds;
- //
- v_DayGrammar = if(v_Days == 1, "DAY", "DAYS");
- v_HourGrammar = if(v_Hours == 1, "HR", "HRS");
- v_MinGrammar = if(v_Minutes == 1, "MIN", "MINS");
- v_SecGrammar = if(v_Seconds == 1, "SEC", "SECS");
- //
- info v_Days + " " + v_DayGrammar + ", " + v_Hours + " " + v_HourGrammar + ", " + v_Minutes + " " + v_MinGrammar + ", " + v_Seconds + " " + v_SecGrammar ;
- // yields: 0 DAYS, 23 HRS, 54 MINS, 11 SECS
Convert Total Minutes to Hours:Minutes
This is for other scenarios:
v_TotalMinutes = 1100; // // determine hours v_Hours = floor(v_TotalMinutes / 60); info "Hours: " + v_Hours; v_Minutes = v_TotalMinutes - (v_Hours * 60); info "Minutes: " + v_TotalMinutes; // // yields: // Hours: 18 // Minutes: 20
- v_TotalMinutes = 1100;
- //
- // determine hours
- v_Hours = floor(v_TotalMinutes / 60);
- info "Hours: " + v_Hours;
- v_Minutes = v_TotalMinutes - (v_Hours * 60);
- info "Minutes: " + v_TotalMinutes;
- //
- // yields:
- // Hours: 18
- // Minutes: 20
Convert Total Seconds to Hours:Minutes
v_TotalMinutes = 66000; // // determine hours v_Hours = floor(v_TotalSeconds / 60 / 60); info "Hours: " + v_Hours; // // determine minutes v_RemainingSeconds = v_TotalSeconds - (v_Hours * 60 * 60); v_Minutes = floor(v_RemainingSeconds / 60); info "Minutes: " + v_Minutes; // // yields: // Hours: 18 // Minutes: 20
- v_TotalMinutes = 66000;
- //
- // determine hours
- v_Hours = floor(v_TotalSeconds / 60 / 60);
- info "Hours: " + v_Hours;
- //
- // determine minutes
- v_RemainingSeconds = v_TotalSeconds - (v_Hours * 60 * 60);
- v_Minutes = floor(v_RemainingSeconds / 60);
- info "Minutes: " + v_Minutes;
- //
- // yields:
- // Hours: 18
- // Minutes: 20