How do I convert a GlideDuration field to number of seconds?

peterdelf
Giga Expert

I'm trying to convert the delivery_time field from a catalogue item to the number of seconds, but I'm going round in circles with GlideDuration and GlideDateTime values.   I know the underlying storage of a GlideDuration object is either seconds or milliseconds.   How do I get to that value?

I've tried the following in background scripts:

var a = new GlideRecord('sc_cat_item');

a.get("0d68ec6edb5c070039f7f70dbf96191d");

gs.print(a.delivery_time.toString());

var b = new GlideDateTime(a.delivery_time.toString());

gs.print(b);

var c = new GlideDuration(0);

gs.print(c.toString());

var d = GlideDateTime.subtract(c, b);

gs.print(d.toString());

Which gives me:

*** Script: 1970-01-03 00:00:00

*** Script: 1970-01-03 00:00:00

*** Script: 1970-01-01 00:00:00

*** Script: 1970-01-03 00:00:00

But I'm going round in circles

Been referring to this - https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDurationGlideDuration_St...

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Peter,



Use the GlideDuration method getByFormat() to format it in to YYYY-MM-dd hh:mm:ss



and then you can feed that to GlideDateTime() and use getNumericValue() to get the number seconds (since 1970).



https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDateTimeGetNumericValue



https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideDurationGetByFormat_Stri...


View solution in original post

10 REPLIES 10

Another quick note, use YYYY-MM-dd HH:mm:ss with uppercase HH. hh uses 12 hour (1-12) intervals to distinguish AM and PM. HH uses 24 hour cycle (0-23)

 

https://docs.servicenow.com/bundle/tokyo-platform-administration/page/administer/time/reference/r_Fo...

 

Good to mention!

shivam_singhal
ServiceNow Employee
ServiceNow Employee

Hi Peter,



I was trying this in my instance and get this (in milliseconds) using the below script.



var a = new GlideRecord('sc_cat_item');


a.get("10b44eb6c6112276005acf13b5c13274");


gs.print(a.sys_updated_on);




var b = new GlideDateTime(a.sys_updated_on);


gs.print(b.getNumericValue());


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. The .getValue() function was crucial.

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

Nisha6
Kilo Explorer

@Chuck Tomasi , Hi Chuck, Could you please help me with the below requirement?

Worked hours  - type decimal

time worked - duration field 

when user enter 5.3 in worked hours that should populate in time worked field like 1 hour 18 min.

find_real_file.png

 

Thanks and Regards,

Nisha