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

Ankur Bawiskar
Tera Patron
Tera Patron

@Muskan Dixit 

try this

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

var userDateFormat = gs.getSession().getUser().getDateTimeFormat();

var date = new GlideDateTime();
date.setDisplayValue(gr.start_date, userDateFormat);

var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
var inSchedule = schedule.isInSchedule(date);

gs.info(inSchedule);

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,

 

Thanks for your response, however this is not working as well.

My use case is to check if planned start date falls in business hours for specific timezone. (for example: i need to check for US time zone if my start date is within schedule).

 

Regards,

Muskan Dixit

@Muskan Dixit 

schedule will have it's own timezone. why to check for US timezone specifically

the GlideSchedule will determine it based on that timezone

Why not have your schedule for US timezone only?

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

Currently i need to check two timezones, US and IST, and i have created schedule for both timezone separately, there is a custom field zone based on that either i'll check my record for IST timezone or US timezone, so based on zone using respective schedule.