Calculate Future Date and Time for a calculation within a Scheduled Job script

jmiskey
Kilo Sage

I am trying to calculate a future date and time for a field that I need to populate in a script found within a Scheduled Job.  Basically, I need to go 3 days from the current date, and choose 5:00 AM that day.

 

So, if today is February 10, 2023, the dynamic date calculation should return February 13, 2023 5:00 AM.

 

I found the "gs.dayAgo" function, but that does not return time.

So if I try to use it like this:

gr.scheduled=gs.daysAgo(-3);

It returns February 13, 2023 12:00 AM instead of February 13, 2023 5:00 AM (right day, wrong time).

How can I get it to add the 5:00 AM time?

 

Thanks

 

1 ACCEPTED SOLUTION

Jamsta1912
Tera Guru

Hi, try this:

var dt = new GlideDate();
dt.addDays(3);

var dt2 = new GlideDateTime();
dt2.setValue(dt.getValue() + ' 05:00:00');
gr.scheduled = dt2;

View solution in original post

10 REPLIES 10

Bert_c1
Kilo Patron

Please see:

 

https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideDateTimeAPI?navFi...

 

You should be able to find a method to assist in setting the desired time for the date value returned.

No joy!  No matter what I do, I cannot seem to get the correct time.  It HAS to be 5:00 AM Local Time.

 

Dates have always been a huge nuisance to work with in ServiceNow, which is why I am requesting assistance!

Try using "add(GlideTime time)" as described in the link I posted. An example is there. 

 

5 am is 18,000 seconds, if you want to use "addSeconds(Number seconds)" described in the same link.  Good luck.

The issue I am having is I am starting with a dynamic date/time, not a hard-coded one like in the examples.  If I grab the current date/time, then I do not necessaarily want to add 5 hours.  I want to get to 5:00 AM on that day.  And of course, the different regional time settings always wreak havic with these calculations.

 

I took a little bit of a different route.  I used this:

var gdt = new GlideDateTime(gs.daysAgo(-3));

and scheduled the job to run at exactly 5:00 AM on Friday, so the new data will be 5:00 AM the following Monday.  Then I don't have to mess with regional time settings and trying to set the time back to midnight and add 5 hours.