Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

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.

18 REPLIES 18

Hi Amit,

 

Thanks for your response, however there are no excluded child schedules or conflicting entries, i am using simple Mon-friday weekday schedule.

 

Regards,

Muskan Dixit

@Muskan Dixit 

 

I tried it using the Schedule 8-5 weekdays and I am able to get the correct output. Refer below screenshot -

 

AmitVerma_0-1735802227038.png

 

Could you please confirm which schedule you are referring to ? Also, is it OOB or a newly created one ? If newly created, could you please share the schedule configuration via screenshots.

Thanks and Regards
Amit Verma

Hi Amit,

 

For my requirement i need to validate start date with two different schedules which are in different timezones ( US & IST).

Also just to understand, in your screenshot it displays time around 11 pm and you said schedule is of 8-5pm, in that case it should return false right or your schedule time is different?

 

Regards,

Muskan Dixit

 

Monique Rogers
Tera Contributor
For anyone who comes across this thread in the future, here's the solution I found:

If I have a schedule that runs from 1500 to 2100 Eastern Time and the logged-in user's session time zone is also Eastern, "GlideSchedule().isInSchedule(dateTime)" functions as expected. It accurately evaluates dates/times within the schedule, including up to the exact end time (e.g., "2026-03-10 21:00:00").

However, if the user's time zone differs from the schedule's time zone, even if you convert your "GlideDateTime" object to the correct time zone, "isInSchedule()" will return "false" when checking times at the exact end of the schedule (e.g., "2026-03-10 21:00:00 ET"). To address this, subtract one second from your "GlideDateTime" before performing the check. When you do so, it will correctly show the time as "in schedule."

 

The script below is provided for anyone that wants to test and validate this theory on their own. 

/**IF THE USER TIME ZONE = SCHEDULE TIME ZONE, BOTH GS.PRINT() MESSAGES WILL SAY TRUE
*   IF THE USERS TIME ZONE != SCHEDULE TIME ZONE:
*   - FIRST GS.PRINT() = FALSE;
*   - SECOND GS.PRINT() = TRUE; 
*/

var schedId = ''; //schedule sys_id
var stringTime = ''; //use any date within schedule, ensuring the time matches the end of schedule in UTC; this is only used to demonstrate the issue
var gdt = new GlideDateTime(stringTime);

var checkSchedule = new GlideSchedule(schedId);
gs.print(checkSchedule.isInSchedule(gdt)); //only shows true if users time zone matches schedules time zone
    gdt.subtract(1000); //subtract one second
gs.print(checkSchedule.isInSchedule(gdt)); //should show true, assuming end time is 1 second before schedules defined end time for that date