GlideSchedule date/time not getting set to local time

gjz1
Giga Expert

We have a requirement to let the CAB manager reject a blackout schedule.  To do that I wrote a business rule that will change the start date/time and end date/time on the schedule entry to the current date/time.  The date/time data types on the schedule entry are GlideSchedule, not GlideDateTime.  I've converted the current date/time to a GlideSchedule date/time but when I update the start/end date/time it doesn't set the local date/time.  For example, 2019-11-13 09:10:38 becomes 2019-11-13 17:20:38.  Would anyone know how I can get the local date/time using a GlideSchedule? 

Below is my business rule and the script log does display the correct current date/time.

Script log:

find_real_file.png

Business Rule:

(function executeRule(current, previous /*null when async*/) {

// Set a GlideSchedule to the current date/time

var currDT = new GlideDateTime();
gs.log('### date: ' + currDT);
var newDate = new GlideSchedule(currDT,'US/Pacific');

// find all schedules associated to the blackout schedule and set the start/end date/time to current date/time.
var sched = new GlideRecord('cmn_schedule_span');
sched.addQuery('schedule', current.sys_id);
sched.query();
while(sched.next()) {
sched.start_date_time = newDate;
sched.end_date_time = newDate;
sched.all_day = false;
sched.u_approval = 'reject';
sched.update();

}

})(current, previous);

1 ACCEPTED SOLUTION

You are correct, I was pointing out it was a time zone issue and what you may need to do to account for it when the user is entering data.

I do not think you are using the GlideSchedule class properly and if all you want is a date/time for right now then all you have to do is 

sched.start_date_time = currDT;
sched.end_date_time = currDT;

Then the system will use the users Time Zone setting to show them the value in there time zone.  If you are trying to find the time something will happen based on a schedule in a time zone then you need to load the schedule into the newDate object then pass it start and end or a start and a duration to find the end D/T.

If you intended to use GlideScheduleDateTime then you need to update the var newDate line for that class.  Then you can do

var currDT = new GlideDateTime();
gs.log('### date: ' + currDT);
var newDate = new GlideScheduleDateTime(currDT.getNumericValue(),'US/Pacific');

View solution in original post

5 REPLIES 5

DrewW
Mega Sage
Mega Sage

ServiceNow does everything in GMT in the background.  So if you have a date/time or just a date that is in the local date/time format you need to use getDisplayValue and setDisplayValue to make sure that the objects get things in the correct time zone.

 

This is a GlideScheduleDateTime, not GlideDateTime.  getDisplayValue doesn't work.

You are correct, I was pointing out it was a time zone issue and what you may need to do to account for it when the user is entering data.

I do not think you are using the GlideSchedule class properly and if all you want is a date/time for right now then all you have to do is 

sched.start_date_time = currDT;
sched.end_date_time = currDT;

Then the system will use the users Time Zone setting to show them the value in there time zone.  If you are trying to find the time something will happen based on a schedule in a time zone then you need to load the schedule into the newDate object then pass it start and end or a start and a duration to find the end D/T.

If you intended to use GlideScheduleDateTime then you need to update the var newDate line for that class.  Then you can do

var currDT = new GlideDateTime();
gs.log('### date: ' + currDT);
var newDate = new GlideScheduleDateTime(currDT.getNumericValue(),'US/Pacific');

Thanks, that was the answer!