How to convert Duration field (days:hours:minutes:seconds) to hours?

jacc_99
Giga Guru

Hi,

I want to convert a Duration field (days:hours:minutes:seconds) like next to hours.

The field is like this picture

find_real_file.png

 

Somebody could help me with that,please?

 

Thanks

1 ACCEPTED SOLUTION

danr2c2
Tera Expert

For anyone else struggling with a similar issue, here's the code I used to add the time in a duration field to the start date/time and then populate the end date/time via a before insert/update business rule.

Converting the duration field to a GlideDateTime and then using the .getValue() function was crucial.  Next you have to use the getNumericValue to get the time in milliseconds.  From there you can manipulate the integer values from there and hopefully get what you're looking for...

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    // calculates the end time of an activity based on the start time and duration

    var startTime = new GlideDateTime(current.variables.access_start.getValue()); // getting start time
    var dur = new GlideDateTime(current.variables.how_long.getValue()); // getting duration and converting to GlideDateTime
    dur = dur.getNumericValue()/1000; // calculating the total duration in seconds
    //gs.addInfoMessage(dur);
	
	startTime.addSeconds(dur); // add the seconds to the start time to calculate end time

	current.variables.access_end = startTime.getValue(); // set the end time
	
    gs.addInfoMessage("End time automatically adjusted based on start time and duration");

})(current, previous);

View solution in original post

7 REPLIES 7

SanjivMeher
Kilo Patron
Kilo Patron

Do you want to display it in hours in that field itself?


Please mark this response as correct or helpful if it assisted you with your question.

asifnoor
Kilo Patron

Hi,

Do you want to calculate and show the actual hours based on the duration selected or just show the hours instead of all the fields?

jacc_99
Giga Guru

I want to transform days, hours, minutes and seconds of the duration field to hours and display this value on another field or keep it in a variable on display Business Rule.

 

Thanks

Hi,

Refer to this link. This might help you.

https://community.servicenow.com/community?id=community_question&sys_id=3be807a1db5cdbc01dcaf3231f96...

Mark the comment as a correct answer and helpful if it answers.