We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

paulmorris
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

paulmorris
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!