How to display minutes in a duration field

Digit
Kilo Guru

I have a business rule that calculates the time (in minutes) a record has been in Open state. Everything is working wonderfully, except that I am now trying to display the minutes value on the form in a duration field, which is taking the minutes value and storing it as hours instead.

Example:

My u_time_in_open field = 6 (6 minutes)

I want my u_open_total field to also equal 6 minutes, but as written, it displays as 6 hours:

Digit_0-1673299449893.png

I'm sure it's a simple thing I'm missing, but I can't think of it.

Any suggestions?

 

Here's my code:

var initTime = current.u_time_in_open; //Initial value (in minutes)

    var startDate = new GlideDateTime(current.u_open_state);
    var endDate = new GlideDateTime();
    var schedule = new GlideSchedule('563653c8db670bc09b71753a8c96194d', 'UTC'); 
    current.u_open_duration = schedule.duration(startDate, endDate);

    //update the aggregated duration and convert from milliseconds to minutes
    current.u_time_in_open = (current.u_open_duration.dateNumericValue() / (60 * 1000)) + initTime; 
    current.u_open_total = new GlideDuration(current.u_time_in_open); //duration

 

2 ACCEPTED SOLUTIONS

jaheerhattiwale
Mega Sage
Mega Sage

@Digit Tried and tested solution. Update the last line of code as below

 

current.u_open_total = "00:"+current.u_time_in_open+":00";
 
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

@Digit Have you tried this? This should definitely work. Please try and mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

5 REPLIES 5

DrewW
Mega Sage
Mega Sage

You need to look at the max_unit attribute for the duration field.  That will tell the system to display the field in total days, hours or minutes.

 

I believe these are all of the valid values

max_unit

Sets the maximum unit of time for the duration. Possible values are max_unit=days,max_unit=hours,max_unit=minutes, and max_unit=seconds.

 

 

Thanks for the suggestion, Drew! 
The problem is that I don't want to limit the duration to minutes. If the time_in_open field = 90 minutes for example; I would want the open_total to display as 1 hour 30 minutes.

jaheerhattiwale
Mega Sage
Mega Sage

@Digit Tried and tested solution. Update the last line of code as below

 

current.u_open_total = "00:"+current.u_time_in_open+":00";
 
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

@Digit Have you tried this? This should definitely work. Please try and mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023