Is it possible to get current time of a different time zone which is not the system time zone which will take into account the daylight savings (if present) there?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2022 02:52 PM
Long question : I want to show on a form if a user is out of working hours in his current time zone. Consider only one timezone for this question for now(Let's say time zone X). For this I am getting the servicenow system time value in a script include and adding the offset hours (+10) to that value to get the current time in time zone X. And then calculate if that current time there is out of working hours or not. I have written this code which works:
var gdt = new GlideDateTime();
var hours = 60 * 60 * 10;
gdt.addSeconds(hours);
var myarr = String(gdt.toString());
myarr = myarr.split(' ');
var time = myarr[1];
time = time.split(':');
var hour = time[0];
var minute = time[1];
var hour1 = parseInt(hour, 10);
var minute1 = parseInt(minute, 10);
if ((hour1 < 16 && hour1 > 8) || ( hour1 == 8 && minute1 > 14) || (hour1 == 16 && minute1 < 31)){
return 'false'; //Not out of working hours
} else {
return 'true'; //out of working hours
}
I know I could use getTime() and compare to glideTime values but it was not working hence used the long method to compare if the current time in time zone X is between 8:15am to 4:30pm . This code is working fine. Problem is that there is a daylight saving going on in Time Zone X, because of which my time calculation of time zone X is 1 hour less than the one being used. I need it to work 24*7*365.
How can I correctly calculate the time in time zone X (system time +10 hours) which will take into account the daylight saving status too?
Labels:
- Labels:
-
Scripting and Coding
2 REPLIES 2

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2022 03:38 PM
Instead of hardcoding calculating working hours in a script, how about using GlideSchedule with timeZone.
GlideSchedule(String sysID, String timeZone)

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2022 04:08 PM
Something like this. Schedule is in "System Scheduler" > "Schedules" > "Schedule". "8-5 weekdays" is one of the OOTB schedule.
function checkInWorkingHours(dateTime, timezone) {
var sched= new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae', timezone); // sys_id of "8-5 weekdays" schedule
var date = new GlideDateTime();
date.setDisplayValue(dateTime);
if (sched.isInSchedule(date))
return true;
else
return false;
}
var dateTime = '2022-03-24 24:30:00';
var timezone = 'America/New_York';
var isInWorkingHours = checkInWorkingHours(dateTime, timezone);
if (isInWorkingHours) {
gs.info("In working hours");
} else {
gs.info("Not in working hours");
}