How to get days from duration

sowmyaj
Giga Expert

Hi All,

I need to get days from duration, I have written the below query and getting the result as "18 Hours 12 Minutes". How do I get the days out of it.

As I'm using scoped application I'm using GlideDateTime.

var gr=new GlideRecord('abc');

gr.query();

if(gr.next()){

  var gdt1 = new GlideDateTime();

  var gdt2 = new GlideDateTime(gr.u_xyz.sys_created_on);

  var dur = new GlideDuration();

  var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2

  var duration= dur.getDisplayValue();

 

  var daysSplit = duration.split('Days');

  gs.info("daysSplit"+daysSplit[0]);

}

Thanks,

Sowmya

3 REPLIES 3

shloke04
Kilo Patron

Hi,



You can calculate the days from duration using the below code:



var curr = duration.dateNumericValue();   // Replace "duration" with your variable Name or fetch current.field_name


var totalMs = curr + 24*60*60*100; //   totalMs is the result in days.



Hope this helps. Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

nayanawadhiya1
Kilo Sage

Hey Sowmya,




You can Glide Date API -


Scoped GlideDate API Reference - ServiceNow Wiki


Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

The GlideDuration class has a getDayPart() method.