The CreatorCon Call for Content is officially open! Get started here.

Convert Time type field value to seconds

swapnil15
Tera Contributor

Hello All,

 

I am using the below Business Rule for getting the time a user works on RITM and CASE.

 

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

    // Check if changes were made to the current RITM or its related Case

    if (current.update() || hasCaseUpdates(current)) {

        var totalTimeWorked = 0;

        var UniqueValue = current.getUniqueValue();

        gs.log('Unique value is :' + UniqueValue);

        var ParentValue = current.request.parent;

        gs.log('Parent value :' + ParentValue);



        var TimeEntryRecord = new GlideRecord('task');

        TimeEntryRecord.addNotNullQuery('time_worked');

        TimeEntryRecord.addQuery('sys_id', 'IN', [UniqueValue, ParentValue]);

        TimeEntryRecord.query();

        while (TimeEntryRecord.next()) {



            var timeWorked = TimeEntryRecord.time_worked.getDisplayValue(); 

            gs.log('Time Worked for ' + TimeEntryRecord.getDisplayValue() + ': ' + seconds + ' seconds');

        }

})(current, previous);

 

 

The script works perfectly fine and gives the time in time_worked variable. The format of time that I get in this variable is seconds, minutes, hours-minutes, days-hours-minutes which it fetches from the time type field everytime. I want to convert all of these formats into seconds. Is there any method which I can use to convert this?

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@swapnil15 

I believe it's a duration type field so use this

use this and then convert milliseconds to seconds

var timeWorked = TimeEntryRecord.time_worked.getNumericValue(); 

timeWorked = timeWorked/1000;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar 
I used the above method and it printed an undefined value.
The field type is Timer (Days, hours, minutes, seconds), not Duration (hh:mm:ss)