Add 1 day to a GlideScheduleDateTime

Kevin Herring
ServiceNow Employee
ServiceNow Employee

I am trying to update some cmn_schedule_span objects (move them forward one day). I understand that start_date_time and end_date_time are the fields I need, but these are of type GlideScheduleDateTime but stored as strings. However, I seem unable to create a 'regular' GlideDateTime which has has nice functions for addition to dates. This is roughly what I am trying to achieve:

var cmn_schedule_span = new GlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', my_cmn_schedule_object);

cmn_schedule_span.query();

while(cmn_schedule_span.next()){
     cmn_schedule_span.start_date_time.addDays(1);
     cmn_schedule_span.update();
}
However, I can't perform arithmetic on start_date_time.
 
I thought this would work:  
while(cmn_schedule_span.next()){
     
     var start = new GlideDateTime(cmn_schedule_span.start_date_time);
    start.addDays(1);
     cmn_schedule_span.start_date_time = start.toString();
     cmn_schedule_span.update();
}
However you can't create a GlideDateTime from a GlideScheduleDateTime. Any thoughts?
1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hello Kevin,

Could you please check with below script:

var cmn_schedule_span = new GlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', cmn_schedule);
cmn_schedule_span.query();
while(cmn_schedule_span.next()){
   var startDateTime = cmn_schedule_span.getDisplayValue("start_date_time");
   gs.print("START DATE: " + startDateTime);
   var gdt = new GlideDateTime();
   gdt.setDisplayValue(startDateTime);
   gdt.addDays(1);
   var newDate = gdt.getDate();
   gs.print("NEW DATE: " + newDate);
   cmn_schedule_span.start_date_time = gdt.getDisplayValue();
   cmn_schedule_span.end_date_time = gdt.getDisplayValue();
   cmn_schedule_span.update();
}

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

 

View solution in original post

5 REPLIES 5

Hello @Kevin Herring 

Just wanted to check with you, if the above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.

regarding the Z at the end to datetime value I tried using different approach but it is not coming, not sure why.

Thanks