Convert GlideDateTime to Days:Hours:Minutes?

jcote
Giga Expert

Since we are unable to have a Service Catalog Variable type as a "Duration" , like we are with field values - I need to convert a duration value to a more readable format.

I have calculated the duration between two GlideDateTime values and have a value of this: "23:24:41";

How do I convert this new value ('23:24:41') to something that would read 23 hours, 24 minutes and 41 seconds?

I could rip it apart with some JavaScript and play around with it that way, but I was wondering if there's a more elegant solution.

Thanks.

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Jamison,



  Here is the easiest way to do this.   Just put in your value in to the GlideDuartion as shown below and use getDisplayValue()


var duration = new GlideDuration('23:24:41');


duration.getDisplayValue(); //this will give you the required format


You can convert it to Days Hours Minutes by using duration.getDisplayValue()


View solution in original post

3 REPLIES 3

ramireddy
Mega Guru

Yes, There is existing method. Here is an example, which calculates span,duration and displaying in the descriptive format as you wanted. You can convert below method as you want.





var gdt = new GlideDateTime("2011-08-31 08:00:00"); //Wednesday


var dur = new GlideDuration();


var span = gdt.getSpanTime(3); //how much time since Monday 00:00:00


dur.setValue(span);


gs.addInfoMessage(dur.getDisplayValue());



GlideDateTime - ServiceNow Wiki


Abhinay Erra
Giga Sage

Jamison,



  Here is the easiest way to do this.   Just put in your value in to the GlideDuartion as shown below and use getDisplayValue()


var duration = new GlideDuration('23:24:41');


duration.getDisplayValue(); //this will give you the required format


You can convert it to Days Hours Minutes by using duration.getDisplayValue()


jcote
Giga Expert

Thanks abhi_r and ramireddy,



I ended up using this the subtract() method here for the solution:


      var start = new GlideDateTime(current.opened_at);


      var end = new GlideDateTime(current.u_resolved_at);


      var xx = GlideDateTime.subtract(start, end);


      gs.log(xx.getDisplayValue(), "JC");



And the output is as expected: 23 Hours 24 Minutes



Thanks.