Total duration of all records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 07:43 AM - edited 10-26-2023 08:03 AM
Need total duration of below records
script:
var val = 0;
var count=new GlideRecord('metric_instance');
count.addEncodedQuery('id=1db320a2970a31108fc572571153af98^value=In Progress');
count.query();
while(count.next()){
val += count.duration.dateNumericValue();
}
gs.print( val);
Results:
*** Script: 48661000
Here, i need in the format of days hours minutes
Can anyone suggest here...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:50 AM
Hi @Charan123 ,
Can u try the below code.
var val = 0;
var count = new GlideRecord('metric_instance');
count.addEncodedQuery('id=1db320a2970a31108fc572571153af98^value=In Progress');
count.query();
while (count.next()) {
val += count.duration.dateNumericValue();
}
// Calculate days, hours, and minutes
var days = Math.floor(val / (24 * 60 * 60 * 1000));
val %= (24 * 60 * 60 * 1000);
var hours = Math.floor(val / (60 * 60 * 1000));
val %= (60 * 60 * 1000);
var minutes = Math.floor(val / (60 * 1000));
gs.print(days + ' days ' + hours + ' hours ' + minute
s + ' minutes');
Thanks,
Danish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:56 AM
Hi @Charan123
You could try it like this:
var val = 0;
var count=new GlideRecord('metric_instance');
count.addEncodedQuery('id=1db320a2970a31108fc572571153af98^value=In Progress');
count.query();
while(count.next()){
val += count.duration.dateNumericValue();
}
gs.print(val);
var jsonDate = convertMillisecondsToDaysHoursMinutesSeconds(val);
gs.print(jsonDate.days + ' days ' + jsonDate.hours + ' hours ' + jsonDate.minutes + ' minutes ' + jsonDate.seconds + ' seconds');
function convertMillisecondsToDaysHoursMinutesSeconds(milliseconds) {
var seconds = milliseconds / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
return {
days: Math.floor(days),
hours: Math.floor(hours % 24),
minutes: Math.floor(minutes % 60),
seconds: Math.floor(seconds % 60),
};
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.