How to convert duration which is of string type to hours format

shreyajoshi
Kilo Expert

Hello All,

Three Fields A,B, C.. Based on the reason field duration value is added into each field. with type "1 hour 44 minutes" e.g

Now I want to go on adding all duration into separate new field 'D'.

Values are in string format for records while saved..by using duration.getDisplayValue().

For adding two duration I can use duration.add(duration2).

 

but how to add string type i.e let's say 5 minutes OR 1 hour 22 minutes  type of duration.

OR how do I convert duration of string type i.e 22 hours 47 minutes into actual hh:mm:ss format?

 

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@shreyajoshi 

please share what values would be present?

there could be values in different format some in minutes, some in hours etc

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

shreyajoshi
Kilo Expert

Hello Ankur,

Let's say,

Field A = 2 Hours 52 Minutes, when records was in A reason the total time is this, later, same records was in B reason hence, total duration for Field B = 5 minutes  for Reason B, later, same records was in C reason hence, for field c duration was 2 days 11 hours. , for Reason C

 

when reason is changing I go on adding a total value of how much time it was in the reason state. SO I need to go on adding.

I am calculating this duration using

    var dur1 = new GlideDateTime(current.pending_since);
    var dur2 = new GlideDateTime();
    dur = GlideDateTime.subtract(dur1, dur2);
    var duration = new GlideDuration(dur).getDurationValue(); => I get in  HH:MM:SS format

 

For setting into actual field i.e A OR B OR C  I use

tm.setValue('A', dur.getDisplayValue());

Now, I need to add total of them.

But while adding when I get values of three they are already in string format i.e 1 hour 22 minutes, so how can I get value of these fields in terms of HH:MM:SS or any other conversion type? to add total of them

Hi,

why not use duration field to store the values?

It would be easier to calculate and sum them up at the end

Anyhow you are calculating the value and storing in string field so use duration field

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

End user would like to get value in the duration style in terms if 2 hours 45 minutes. Hence displaying it in this way.

 

On the form duration field shows in below format and on list view in string format. can we have in sync on form and list view