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

First, please see:

 

https://www.servicenow.com/community/now-platform-articles/10-tips-for-writing-a-quality-community-q...

 

And post your entire script. Others here may have better ideas.

Bert_c1
Kilo Patron
var dt = new GlideDateTime();
gs.info("script include: dt = " + dt + ".");
dt.addDays(-3);
var pdt = dt.getDate();
gs.info("script include: pdt = " + pdt + ".");

var desiredDateTime = new GlideDateTime(pdt+" 05:00:00");
gs.info("script include: desiredDateTime = " + desiredDateTime + ".");

/* following doesn't work
gr.scheduled=gs.daysAgo(-3);
gs.info("gr.scheduled = " + gr.scheduled + ".");
*/

Test in the script of a Scheduled Job set to run "On Demand". See results in the Script Log Statements module.

This is also helpful and very close to what I needed.  Just need to change this part:

dt.addDays(-3);

to this:

dt.addDays(3);

as I originally had

.daysAgo(-3)

so if you switch the function, you also need to switch the sign.

 

Thanks for your help!

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;

Yes, that is exactly what I was looking for.

I think the problem I was having what I was using "GlideDateTime" for my first date, so adding 5 hours added 5 hours to the current time, and not from midnight.

 

Thank you!