- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2017 09:02 AM
Hello,
We have a requirement to take a planned start date/time and calculate the date/time for 24 hours prior.
We also have a requirement to take a planned end date/time and calculate the date/time for 24 hours after, but I have gotten this far yet.
We don't care about weekends, holidays, work day hours. Just simple date/time.
I tried multiple ways of doing this after reading multiple articles, and I can't seem to get this right. Here is the code I ran in a background script to test:
var rec = new GlideRecord("change_request");
rec.addEncodedQuery("active=true^state=6^u_executed_code=0^u_contingency_invoked=false^EQ");
rec.query();
while (rec.next()) {
var ps = rec.start_date;
ps = ps - gs.daysAgo(-1);
gs.print('start date ' + rec.start_date);
gs.print('start date 24 hours ago ' + ps);
}
What is returned is:
How to I get the date minus 1 day into date format?
Thank you in advance.,
Laurie
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2017 09:07 AM
Hi,
Please see if this code helps you to get the correct days
var rec = new GlideRecord("change_request");
rec.addEncodedQuery("active=true^state=6^u_executed_code=0^u_contingency_invoked=false^EQ");
rec.query();
while (rec.next()) {
var ps = new GlideDateTime(rec.start_date.getDisplayValue());
ps.addDays(1);
var dateafter =ps.getDate();
ps.addDays(-2);
var datebefore =ps.getDate();
gs.print('Date After ' +dateafter); //Date + 1 day
gs.print('Date Before' +datebefore); //Date - 1 day
}
Thanks
Please Hit like, Helpful or Correct depending on the impact of the response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2017 09:07 AM
Hi,
Please see if this code helps you to get the correct days
var rec = new GlideRecord("change_request");
rec.addEncodedQuery("active=true^state=6^u_executed_code=0^u_contingency_invoked=false^EQ");
rec.query();
while (rec.next()) {
var ps = new GlideDateTime(rec.start_date.getDisplayValue());
ps.addDays(1);
var dateafter =ps.getDate();
ps.addDays(-2);
var datebefore =ps.getDate();
gs.print('Date After ' +dateafter); //Date + 1 day
gs.print('Date Before' +datebefore); //Date - 1 day
}
Thanks
Please Hit like, Helpful or Correct depending on the impact of the response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2017 09:34 AM
Thank you so much, Alikutty!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2017 09:38 AM
Hi Laurie,
If your issue is resolved, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list. If you are viewing this from the community inbox you will not see the correct answer button. If so, please review How to Mark Answers Correct From Inbox View.
https://community.servicenow.com/docs/DOC-5601
Thanks
Please Hit like, Helpful or Correct depending on the impact of the response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2024 08:00 PM
Hi
Could you please help me with the below quey
Re: How to subtract days from a schedule - ServiceNow Community