Subtract two durations fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2017 02:56 PM
Hi, could anyone kindly assist on how to subtract two duration fields? Below is my code.
var tw = new GlideAggregate("metric_instance");
var metricSysID = 'ebff7bda0f920300ea7087ece1050efc';
tw.addQuery('id', '3149a00c0f560300ea7087ece1050eb0');
//tw.addQuery('id', current.sys_id);
tw.addQuery('value', 'Work in Progress');
tw.addAggregate("SUM", "duration");
tw.groupBy("id");
tw.query();
if (tw.next()) {
var timeworked = tw.getAggregate("SUM", "duration");
var dur = gs.dateDiff(tw.opened_at, gs.nowDateTime());
//var diff = (parseInt(timeworked) - parseInt(dur));
var diff = gs.dateDiff(timeworked, dur);
gs.print(" Duration1: " + dur);
gs.print(" Duration2: " + timeworked);
gs.print("Difference: " + diff);
}
*** Script: Duration1: 23:59:59
*** Script: Duration2: 00:08:13
*** Script: Difference: 00:00:00

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2017 03:35 PM
Please see below post which has solution for duration calculation
Addition & subtraction of two duration field
Regares,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2017 03:39 PM
Hi Sachin,
Thanks. I had gone to that post but they weren't able to subtract the durations also. see a portion of the code here, It's printing NaN.
{
var grd_duration= grd.getAggregate('SUM', 'duration');
gs.log('Value of Duration:' + grd_duration); // This one is printing correct duration
duration = duration - grd_duration;
gs.log('Total Value :' + duration); // This one is printing as NaN.
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2017 03:48 PM
please try like,
duration = parseInt(duration) - parseInt(grd_duration);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2017 08:15 PM
Hi Shishir,
This is what I am getting:
*** Script: Duration1: 00:06:33
*** Script: Duration2: 23:59:59
*** Script: Difference: 23