Date minus 1 Day Calculation

Laurie Marlowe1
Kilo Sage

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:

Capture.JPG

How to I get the date minus 1 day into date format?

Thank you in advance.,

Laurie

1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

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


View solution in original post

4 REPLIES 4

Alikutty A
Tera Sage

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


Thank you so much, Alikutty!


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.


JPSS
Tera Contributor

Hi

 

Could you please help me with the below quey

Re: How to subtract days from a schedule - ServiceNow Community