- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 07:41 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 04:12 PM
Hi, try this:
var dt = new GlideDate();
dt.addDays(3);
var dt2 = new GlideDateTime();
dt2.setValue(dt.getValue() + ' 05:00:00');
gr.scheduled = dt2;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 07:57 AM
Please see:
You should be able to find a method to assist in setting the desired time for the date value returned.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 08:11 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 09:36 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 09:46 AM
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.