How to get GMT value of a dates?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 02:30 PM
From the below script i am trying to get the overlap dates of a schedule
It is providing me the display values of the schedule date fields, Display value not getting adjusted according to user timezone.
var ss = new GlideRecord('cmn_schedule_span'); ss.addQuery('schedule', 'sys ids of the maintenance schedules'); ss.orderBy('start_date_time'); ss.query(); // consider i have 2 maitenace schedules while (ss.next()) { var timeZone = gs.getSession().getTimeZoneName(); var thisSchedule = new GlideSchedule(); thisSchedule.setTimeZone(timeZone); gs.log("addHoliday to Schedule " + ss.name); thisSchedule.addTimeSpan(ss); Start from Today var gd = new GlideDate(); var gdt = new GlideDateTime(); gdt.setNumericValue(gd.getNumericValue()); / GDT (GlideDateTime) // Start Datevar sd = new GlideDateTime(); sd.setValue(gdt.getDate() + " 00:00:00");
/ End Date var ed = new GlideDateTime(); ed.setValue(gdt.getDate() + " 23:59:59"); ed.addDaysUTC(730); // Look 2 years ahead (this could be anything)
var ssd = new GlideScheduleDateTime(sd); // Start Day ssd.setTimeZone(timeZone);
var sed = new GlideScheduleDateTime(ed); // End Day sed.setTimeZone(timeZone);
var scheduleMap = thisSchedule.getTimeMap(sd, ed, timeZone); var span = new GlideScheduleDateTimeSpan(ssd, sed); var thisMap = new GlideScheduleTimeMap(); thisMap.addInclude(span); thisMap.buildMap(timeZone); overlaps = scheduleMap.overlapsWith(thisMap, timeZone); overlaps.buildMap(timeZone); while (overlaps.hasNext()) { var overlapSpan = overlaps.next(); gs.log("overlapSpan Dates: " + overlapSpan.getStart() + " - " + overlapSpan.getEnd()); } }
output:
schedule 1: *** Script: overlapSpan Dates: 2024-03-27 01:00:00 - 2024-03-27 23:00:00 *** Script: overlapSpan Dates: 2024-04-03 01:00:00 - 2024-04-03 23:00:00 schedule 2: *** Script: overlapSpan Dates: 2024-03-21 07:00:00 - 2024-03-21 21:00:00 *** Script: overlapSpan Dates: 2024-04-21 07:00:00 - 2024-04-21 21:00:00
EXPECTED OUTPUT:
GMT values of the schedule/Display value according to usertimezone
Reason for conversion
the above script not working based on user timezone. it is not displaying the overlap span according to user timezone
my system time zone is Europe/Dublin so if can at least get the GMT of Europe/Dublin , i can adjust the values to user timezone
please provide a solution.
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 03:20 PM
Hi,
Can you try to test below script and see
var ss = new GlideRecord('cmn_schedule_span');
ss.addQuery('schedule', 'sys ids of the maintenance schedules');
ss.orderBy('start_date_time');
ss.query(); // consider I have 2 maintenance schedules
while (ss.next()) {
var timeZone = gs.getSession().getTimeZoneName();
var thisSchedule = new GlideSchedule();
thisSchedule.setTimeZone(timeZone);
gs.log("addHoliday to Schedule " + ss.name);
thisSchedule.addTimeSpan(ss);
// Start from Today
var gd = new GlideDate();
var gdt = new GlideDateTime();
gdt.setNumericValue(gd.getNumericValue());
// Start Date
var sd = new GlideDateTime();
sd.setValue(gdt.getDate() + " 00:00:00");
// End Date
var ed = new GlideDateTime();
ed.setValue(gdt.getDate() + " 23:59:59");
ed.addDaysUTC(730); // Look 2 years ahead (this could be anything)
var ssd = new GlideScheduleDateTime(sd); // Start Day
ssd.setTimeZone(timeZone);
var sed = new GlideScheduleDateTime(ed); // End Day
sed.setTimeZone(timeZone);
var scheduleMap = thisSchedule.getTimeMap(sd, ed, timeZone);
var span = new GlideScheduleDateTimeSpan(ssd, sed);
var thisMap = new GlideScheduleTimeMap();
thisMap.addInclude(span);
thisMap.buildMap(timeZone);
overlaps = scheduleMap.overlapsWith(thisMap, timeZone);
overlaps.buildMap(timeZone);
while (overlaps.hasNext()) {
var overlapSpan = overlaps.next();
var startGMT = new GlideDateTime(overlapSpan.getStart());
startGMT.setTZ("GMT");
var endGMT = new GlideDateTime(overlapSpan.getEnd());
endGMT.setTZ("GMT");
gs.log("overlapSpan Dates: " + startGMT.getDisplayValue() + " - " + endGMT.getDisplayValue());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 06:42 PM
Hi @surya76
What is your requirement? ServiceNow OOB stored the date/time in UTC timezone and displays the user to timezone selected by the user. You may probably need not to convert anything.
Regards,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2024 10:01 PM
The above script is to find next maintenance schedules available for the CI
"ServiceNow OOB stored the date/time in UTC timezone"
Yes i read that in some of the articles but the mentioned script does not not giving the output based on the user time zone .
it is giving the display dates of the schedules even though i did not use get display value
so if i need to fix this
if any user in any time zone runs this script iam getting the same display value of the schedules.
if that is the case i cant able to show the exact dates of the window that they have
expected solution
need the display value of the dates according to the user timezones