Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Total duration of all records

Charan123
Tera Expert

Need total duration of below records

bhavi_0-1698331183812.png


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...

2 REPLIES 2

Danish Bhairag2
Tera Sage

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

 

Peter Bodelier
Giga Sage

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),
  };
}

PeterBodelier_0-1698339367056.png

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.