Check if date falls under schedule for specific timezone

Muskan Dixit
Tera Contributor

Hi Community,

 

I need help with the code to check if date falls under schedule for specific timezone.

I am using isInSchedule() method, for some reason its not giving correct output, I have used this several times in past not sure why its giving me hard time now, may be i am missing some small point.

 

Tried various approaches:-

 

1.Basic approach to use getDisplayValue to get user's session timezone.

 

Schedule: HongKong, 8 to 17:00 weekday
Input Date: 2025-01-02 16:00:00

var gr = new GlideRecord("change_request");
gr.get("Change Sys_id");
var planStartDate= gr.start_date.getDisplayValue();

var startDate = new GlideDateTime(planStartDate);
gs.print(startDate);  --> This is displaying correct input value

var sch= new GlideSchedule("090eecae0a0a0b260077e1dfa71da828");
gs.print(sch.isInSchedule(startDate)); --> This is giving output as "false"

 

2. Another approach is to convert time zone and then give in isInSchedule method, for this i tried setTZ() and setTimeZone() both method, both are converting properly and getting output as per timezone only however schedule function is still not returning correct output.

 

Please let me know what i am missing here.

17 REPLIES 17

@Muskan Dixit 

then based on that field select the schedule and pass that sysId and check if the date falls in schedule

what's the challenge?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Muskan Dixit 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, I understand its a simple check, not sure if i am missing something here.

 

My approach is to convert start date based on the required timezone and then compare it with the respective schedule(in schedule also timezone is given). for that i tried below ways, i am able to convert date in timezone properly however it seems like isInSchedule is validating in UTC timezone and thats why returning false.

 

1. Timezone converstion via setTimeZone

 

var gr = new GlideRecord("change_request");

gr.get("sys_id");

var planStartDate= gr.start_date;

var timeZone ="IST";

 

var startDate = new GlideDateTime(planStartDate);

gs.print(startDate); //UTC Date

startDate.setTimeZone(timeZone);

var startDateInTz = startDate.getDisplayValue(); //string value

var newStartDate= new GlideDateTime(startDateInTz); //convert to object

gs.print("newStartDate "+newStartDate); //getting date as per timezone

var sch= new GlideSchedule("sys_id"); using IST schedule

gs.print(sch.isInSchedule(newStartDate)); //schedule is 20:30 -5:30am IST but on checking i realized its giving true when newStartDate is between 15:00 - 0:00 so basically -5:30 (UTC time).

 

2. Timezone converstion via setTz - Here also same timezone conversion is working properly but schedule is not giving proper output

 

var gr = new GlideRecord("change_request");
gr.get("sys_id");
var planStartDate= gr.start_date;
var time = new GlideDateTime(planStartDate);
gs.info('UTC time is->' + time);
var targetTimezone = 'IST';
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
gs.info('IST time is->' + time); //getting correct time in IST
var sch= new GlideSchedule("sys_id"); //using IST schedule
gs.print(sch.isInSchedule(time)); //not returning correct value.
 
3. I also tried to give date without any conversion (in UTC format only) as from testing it seemed like isInSchedule checks in UTC time, but this is also failing in some scenarios for US schedule.
 
Not sure if i am complicating things here all i want to validate that if date falls within required timezone schedule or not.

 

 

 

@Muskan Dixit 

but in your schedule what's the timezone?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

I created two schedules, in one US timezone is given and in 2nd one IST.

I need to validate if start date is in business hours as per the given schedule.

Based on condition i need to validate start date with either US schedule or IST one.

 

Regards,

Muskan Dixit