Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Convert seconds to GlideDuration object

Alex Ng
Tera Contributor

Hello Community,

Does anyone knows how to I convert seconds into GlideDuration object? I need the seconds to write to GlideDuration field type in a table.

The seconds are passed in by DurationCalculator object as below:

var dc = new DurationCalculator();
var duration = dc.calcScheduleDuration("2012-04-10 08:00:00","2012-04-14 06:00:00"));

I need the duration to be stored in GlideDuration object but can't seems to find the right way to do so.
Can anyone advise on this?

Thank you.

 

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage
var dc = new DurationCalculator();
var durationInSeconds = dc.calcScheduleDuration("2012-04-10 08:00:00","2012-04-14 06:00:00"); //Returns seconds
var durationInMs = durationInSeconds * 1000
var gDuration = new GlideDuration(durationInMs ); //Takes MS as paramater

current.setValue('your_duration_field', gDuration.getValue() ); //Set a duration field

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

2 REPLIES 2

The SN Nerd
Giga Sage
Giga Sage
var dc = new DurationCalculator();
var durationInSeconds = dc.calcScheduleDuration("2012-04-10 08:00:00","2012-04-14 06:00:00"); //Returns seconds
var durationInMs = durationInSeconds * 1000
var gDuration = new GlideDuration(durationInMs ); //Takes MS as paramater

current.setValue('your_duration_field', gDuration.getValue() ); //Set a duration field

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thank you Paul! 🙂 I reused a part of this solution. Really helpful!