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

Mohith Devatte
Tera Sage
Tera Sage

hello @Kevin Herring ,

try this usually those two fields are stored in this format 

20220421T190000Z -->which is like date+T+Time is the format in which the concatenation is happening OOB 

See below screenshot:

find_real_file.png

you can try this script 

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()){
var gdt=new GlideDateTime(cmn_schedule_span.start_date_time);
gdt.addDays(1);
var added = gdt;

var spt=added.toString().split(' ');
var datespt=spt[0];
var timespt=spt[1];


var dateCombo =datespt.split('-');
var timeCombo=timespt.split(':');

var finalValue = dateCombo[0]+dateCombo[1]+dateCombo[2]+"T"+timeCombo[0]+timeCombo[1]+timeCombo[2];
cmn_schedule_span.start_date_time = finalValue;
 cmn_schedule_span.update();
}




Output:

find_real_file.png

Hope this helps 

Mark my answer correct if this helps you

 

Kevin Herring
ServiceNow Employee
ServiceNow Employee

Thanks for your help Mohith, but unfortunately that doesn't work. The following line doesn't result in a valid gdt:

var gdt=new GlideDateTime(cmn_schedule_span.start_date_time);

The following code:

var cmn_schedule_span = newGlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', cmn_schedule);

cmn_schedule_span.query();

while(cmn_schedule_span.next()){
   var start = newGlideDateTime(cmn_schedule_span.start_date_time);
   gs.info('cmn_schedule_span.start_date_time: [' + cmn_schedule_span.start_date_time + ']');
   gs.info('start: [' + start + ']');
}

results in the following output:

*** Script: cmn_schedule_span.start_date_time: [20180418T110000Z]
*** Script: start: []

i.e. the GlideDateTime has not been created.

Thanks, Kevin

 

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

 

Thanks guys, we're nearly there! I updated your code slightly Mahendra:

    while(cmn_schedule_span.next()){
        gs.print("cmn_schedule_span.start_date_time: " + cmn_schedule_span.start_date_time);
        gs.print(cmn_schedule_span.start_date_time.getTimeZone());
        var startDateTime = cmn_schedule_span.getDisplayValue("start_date_time");
        gs.print("startDateTime: " + startDateTime);
        var gdt = new GlideDateTime();
        gdt.setDisplayValue(startDateTime);
        gs.print("gdt: " + gdt);
        gdt.addDays(1);
        gdt.addMinutes(1);
        gs.print("Updated gdt: " + gdt);
        cmn_schedule_span.start_date_time = new GlideScheduleDateTime(gdt.toString());
        gs.print('updated cmn_schedule_span.start_date_time:' + cmn_schedule_span.start_date_time)
        gs.print(cmn_schedule_span.start_date_time.getTimeZone());
    }
Which results in the following output:
 
*** Script: cmn_schedule_span.start_date_time: 20180418T110000Z
*** Script: undefined
*** Script: startDateTime: 2018-04-18 12:00:00
*** Script: gdt: 2018-04-18 11:00:00
*** Script: Updated gdt: 2018-04-19 11:00:00
*** Script: updated cmn_schedule_span.start_date_time:20180419T110000
*** Script: undefined
 
The time seems to match perfectly, although it's weird that the addMinutes function does nothing (I only added this as a sanity check, any ideas why?). The only other difference is that the original start_date_time has a 'Z' at the end, but the final version does not. I believe this refers to the timezone, so I will need to test in other timezones. Thanks