Adding date to current date

eashwar
Tera Contributor

Folks, i have an automated report requirement where i need to write an script to automate planned start date and end date

Requirement : My start date should always be Wednesday and end date is always next wednesday

For Eg :

if script is ran today. it should mark planned start day as next wednesday and end date as wednesday following after that

Today Date : 3/3

if i run script today : Planned start should be : 3/10

Planned end date 3/17

Need your experts help in incremental dates

var today = new Date();

var thisDay = today.getDay(); //returns 0 for Sunday, 1 for Monday, etc. thru 6 for Saturday.

if (thisDay > 3)

var startDate = new GlideDateTime();

StartDate.setDisplayValue(gs.beginningOfNextWeek()); // Here i would like to add 3 more days and make it a wednesday ?

var endDate = new GlideDateTime();

StartDate.setDisplayValue(gs.beginningOfNextWeek()); // Here i would like to add 3 more days and make it next wednesday ?

if (thisDay < 3)

var startDate = new GlideDateTime();

StartDate.setDisplayValue(gs.beginningOfThistWeek()); // Here i would like to add 3 more days and make it a wednesday ?

var endDate = new GlideDateTime();

StartDate.setDisplayValue(gs.beginningOfThisWeek()); // Here i would like to add 7 more days and make it   next wednesday ?

if (thisDay == 3)

var startDate = new GlideDateTime();

StartDate.setDisplayValue(gs.now()); // make it current date

var endDate = new GlideDateTime();

StartDate.setDisplayValue(gs.beginningOfNextWeek()); // Here i would like to add 3 more days and make it   next wednesday ?

4 REPLIES 4

TrevorK
Kilo Sage

It sounds like your start date has to be the next Wednesday, however it cannot start on any other day. With that in mind, I think your logic is:


1) Check what day of the week it is: getDayOfWeek()


2) Do the math for when the next Wednesday is: addDays() to make you reach the next Wedensday based on what day of week it is


3) Now that we have the next Wednesday, we can use addWeeks() to bring us to the next couple Wednesdays



This should allow you to find the next Wednesday (if it is not already Wednesday), then schedule accordingly.



Remember that in Eureka and beyond the dates often have "UTC" and "LocalTime" - make sure to understand the difference and pick appropriately. You can find the API (with all the items I mention above) here: GlideDateTime - ServiceNow Wiki



If you need anymore help let me know!


eashwar
Tera Contributor

Thank you Trevor.


I almost managed to write below scipt but i am stuck to write between query for dates. can you help



Comments on line : // This line needs to be corrected



var today = new Date();


var thisDay = today.getDay();


var StartDate = new GlideDateTime();





if (thisDay == 0)


  {


  StartDate.addDays(3); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("1st one " + StartDate);


        }


        else if (thisDay == 1)


  {


  StartDate.addDays(2); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("2nd one " + StartDate);


        }


        else if (thisDay == 2)


  {


  StartDate.addDays(1); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("3rd one " + StartDate);


        }


        else if (thisDay == 3)


  {


  StartDate.addWeeks(1); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("4th one " + StartDate);


        }


        else if (thisDay == 4)


  {


  StartDate.addDays(6); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("5th one " + StartDate);


        }


        else if (thisDay == 5)


  {


  StartDate.addDays(5); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("6th one " + StartDate);


        }


        else if (thisDay == 6)


  {


  StartDate.addDays(4); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("7th one " + StartDate);


        }






var grChange = new GlideRecord("change_request");


//grChange.addQuery("number", "CHG0037950");


grChange.addActiveQuery();


grChange.addQuery("start_date", 'BETWEEN', StartDate , StartDate.addWeeks(1));   // This line needs to be corrected


grChange.query();


gs.print('Change Query: ' + grChange.getEncodedQuery() + ' = ' + grChange.getRowCount());






while(grChange.next())


{


  //gs.print("type " + grChange.type);


  //gs.print("State " + grChange.state);




  if(grChange.type == "Comprehensive")


  {


  if(grChange.state != 9 || grChange.state != 62 || grChange.state != 10 || grChange.state != 8 || grChange.state != 7 )


  {



  gs.print("number " + grChange.number + " state: " + grChange.state + " type " + grChange.type + " planned start date " + grChange.start_date ) ;





  }


  }




}


I am not sure that GlideRecord supports BETWEEN - I have personally never used it. If it's not working and you cannot find documentation that states it is supported, I think there is a good chance it is not.



You can use an encoded query string: Encoded Query Strings - ServiceNow Wiki



Or, it looks like sabell2012 has written an excellent post on extending GlideRecord to allow BETWEEN to work: Mini-Lab: GlideRecord Between Query Extension


My thoughts are that if Steve went to all that trouble, it probably doesn't exist.




If Steve's implementation is too much for you, the encoded query string will work perfect.



Hope that helps! Let me know if I can be of any more assistance.



EDIT: I just realized you could also do it the "sloppy" way:


grChange.addQuery("start_date", "<=", StartDate.addWeeks(1));  


grChange.addQuery("start_date", ">=", StartDate);


This would obviously work as well.


eashwar
Tera Contributor

Thanks Buddy .. Finally i was able to get it






var today = new Date();


var thisDay = today.getDay();


var StartDate = new GlideDateTime();


var endCalc = new GlideDateTime();


var endDate = new GlideDateTime();












if (thisDay == 0)


  {


  StartDate.addDays(3); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("1st one " + StartDate);


        }


        else if (thisDay == 1)


  {


  StartDate.addDays(2); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("2nd one " + StartDate);


        }


        else if (thisDay == 2)


  {


  StartDate.addDays(1); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("3rd one " + StartDate);


        }


        else if (thisDay == 3)


  {


  StartDate.addWeeks(1); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("4th one " + StartDate);


        }


        else if (thisDay == 4)


  {


  StartDate.addDays(6); // Here i would like to add 3 more days and make it a wednesday ?


  endDate.addDays(13)


  gs.print("5th one " + StartDate);


  gs.print("5th one end date " + endDate);


        }


        else if (thisDay == 5)


  {


  StartDate.addDays(5); // Here i would like to add 3 more days and make it a wednesday ?



  gs.print("6th one " + StartDate);




        }


        else if (thisDay == 6)


  {


  StartDate.addDays(4); // Here i would like to add 3 more days and make it a wednesday ?


  gs.print("7th one " + StartDate);


        }






var grChange = new GlideRecord("change_request");


//grChange.addQuery("number", "CHG0037950");


grChange.addActiveQuery();


//grChange.addQuery("start_date", 'BETWEEN', StartDate , StartDate.addWeeks(1));


grChange.query();


gs.print('Change Query: ' + grChange.getEncodedQuery() + ' = ' + grChange.getRowCount());






while(grChange.next())


{


  //gs.print("type " + grChange.type);


  //gs.print("State " + grChange.state);




  if(grChange.type == "Comprehensive")


  {


  if(grChange.state != 9 || grChange.state != 62 || grChange.state != 10 || grChange.state != 8 || grChange.state != 7 )


  {



  if(grChange.start_date >= StartDate && grChange.start_date <= endDate )


  {


                    gs.print("number " + grChange.number + " state: " + grChange.state + " type " + grChange.type + " planned start date " + grChange.start_date ) ;


  }





  }


  }




}