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

Hi asifnoor,

 

I did it last week but it does't work in Madrid version.

 

 

Thanks for your answer

 

 

Hi,

Could you let me know the issue that you are facing.

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);