- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi, I have a business rule where I am trying to calculate the following on a project:
1. Project During between start and end dates
2. Remaining Duration between now and project end date
Example start date 01/09/2025 00:00:00 & End Date 31/10/2025 00:00:00
The Project Duration is returning a figure of (60 Days 16 Hours)
The Remain Duration figure changes every time I save but not in the correct way, its random (63 Days 23 Hours 36 Minutes then 63 Days 3 Hours 1 Minute then 63 Days 33 Minutes then 63 Days 22 Hours 49 Minutes)
Obviously my calculations are wrong somewhere, can anyone assist and point out where
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Glad to know that my script worked.
I believe I shared the working code and you can enhance it further based on your developer skills and experience.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Glad to know that my script worked.
I believe I shared the working code and you can enhance it further based on your developer skills and experience.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I use the following logic:
// Calculating a duration field based on two date-time values
var testRec = new GlideRecord('u_test_table');
testRec.query();
while (testRec.next()) {
var dateDiff = testRec.u_end_date.dateNumericValue() - testRec.u_start_date.dateNumericValue();
gs.info('Start dateTime: ' + testRec.u_start_date + ', End dateTime: ' + testRec.u_end_date);
var numSecsDays = 24 * 60 * 60;
numSecsHour = 60 * 60;
numSecsMinute = 60;
var numSecs = dateDiff / 1000;
var numDays = Math.floor(numSecs / numSecsDays);
var remSecs = numSecs - (numDays * numSecsDays);
var numHours = Math.floor(remSecs / numSecsHour);
remSecs -= (numHours * numSecsHour);
var numMinutes = Math.floor(remSecs /numSecsMinute);
remSecs -= (numMinutes * numSecsMinute);
gs.info('Days: ' + numDays + ', hours: ' + numHours + ', minutes: ' + numMinutes + ', seconds: ' + remSecs);
}
the results:
*** Script: Start dateTime: 2025-08-21 16:11:14, End dateTime: 2025-08-24 15:14:14
*** Script: Days: 2, hours: 23, minutes: 3, seconds: 0
*** Script: Start dateTime: 2025-05-24 20:57:58, End dateTime: 2025-05-26 21:58:59
*** Script: Days: 2, hours: 1, minutes: 1, seconds: 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi
Ankur's script worked but I will also try yours to see if it gives me better results where I have the 1 issue
Thanks