Inconsistent results when checking dates against a schedule using GlideDate

rishabh jain3
Tera Guru

I'm trying to verify whether a specific date falls within an existing schedule in ServiceNow. However, I'm seeing different results depending on whether I manually input the date or use GlideDate() to get the current date. Here's the script I'm using:

var dateTime1 = new GlideDate('2025-07-03'); // today's date
var dateTime2 = new GlideDate(); // Retrieves the current date
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // sys_id of my schedule

var inSchedule1 = schedule.isInSchedule(dateTime1); // Returns true or false
var inSchedule2 = schedule.isInSchedule(dateTime2); // Returns true or false

gs.print(inSchedule1 + dateTime1);
gs.print(inSchedule2 + dateTime2);

Output:
*** Script: false2025-07-03
*** Script: true2025-07-03


As you can see, both dates appear the same (2025-07-03), but the isInSchedule method gives different results. I'm trying to understand why this happens. Could it be due to time values not being set in GlideDate, or is there something else I might be missing?

Any insights or suggestions would be appreciated.

 

rishabhjain3_0-1751555464648.png

 

 

2 ACCEPTED SOLUTIONS

Santosh Oraon
Tera Expert

Hi @rishabh jain3 , The problem is with your current code is that the isInSchedule() method expects a date and time.
Here is breif information:-

The issue in your code, resulting in false for dateTime1 and true for dateTime2 even though they represent the same date (2025-07-03), lies in the type of object you are passing to schedule.isInSchedule() and the time component implicitly or explicitly associated with them.

Here's a breakdown:

  1. var dateTime1 = new GlideDate('2025-07-03');

    • You are creating a GlideDate object. A GlideDate object only stores the date component (year, month, day). It does not contain any time information.

    • When you pass dateTime1 (a GlideDate object) to schedule.isInSchedule(), the system might default its time component to 00:00:00 (midnight) for the purpose of checking against the schedule.

    • If your schedule "090eecae0a0a0b260077e1dfa71da828" does not have an entry for 2025-07-03 at precisely midnight (or if midnight is outside its defined working hours for that day), isInSchedule() will return false.

  2. var dateTime2 = new GlideDate();

    • You are creating another GlideDate object, but this time you are initializing it without any arguments. When initialized this way, new GlideDate() retrieves the current date.

    • Crucially, when isInSchedule() is called with a GlideDate object that represents the current date, it often implicitly uses the current time for the check.

    • Since it is currently Thursday, July 3, 2025, at 9:32:32 PM IST, and assuming your schedule has active hours at this time on July 3rd, isInSchedule(dateTime2) will likely return true.

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@rishabh jain3 

the method works with GlideDateTime object and not with GlideDate

try this

var dateTime1 = new GlideDateTime('2025-07-03 00:00:00'); // today's date
var dateTime2 = new GlideDateTime(); // Retrieves the current date
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // sys_id of my schedule

var inSchedule1 = schedule.isInSchedule(dateTime1); // Returns true or false
var inSchedule2 = schedule.isInSchedule(dateTime2); // Returns true or false

gs.print(inSchedule1 + dateTime1);
gs.print(inSchedule2 + dateTime2);

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

View solution in original post

2 REPLIES 2

Santosh Oraon
Tera Expert

Hi @rishabh jain3 , The problem is with your current code is that the isInSchedule() method expects a date and time.
Here is breif information:-

The issue in your code, resulting in false for dateTime1 and true for dateTime2 even though they represent the same date (2025-07-03), lies in the type of object you are passing to schedule.isInSchedule() and the time component implicitly or explicitly associated with them.

Here's a breakdown:

  1. var dateTime1 = new GlideDate('2025-07-03');

    • You are creating a GlideDate object. A GlideDate object only stores the date component (year, month, day). It does not contain any time information.

    • When you pass dateTime1 (a GlideDate object) to schedule.isInSchedule(), the system might default its time component to 00:00:00 (midnight) for the purpose of checking against the schedule.

    • If your schedule "090eecae0a0a0b260077e1dfa71da828" does not have an entry for 2025-07-03 at precisely midnight (or if midnight is outside its defined working hours for that day), isInSchedule() will return false.

  2. var dateTime2 = new GlideDate();

    • You are creating another GlideDate object, but this time you are initializing it without any arguments. When initialized this way, new GlideDate() retrieves the current date.

    • Crucially, when isInSchedule() is called with a GlideDate object that represents the current date, it often implicitly uses the current time for the check.

    • Since it is currently Thursday, July 3, 2025, at 9:32:32 PM IST, and assuming your schedule has active hours at this time on July 3rd, isInSchedule(dateTime2) will likely return true.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@rishabh jain3 

the method works with GlideDateTime object and not with GlideDate

try this

var dateTime1 = new GlideDateTime('2025-07-03 00:00:00'); // today's date
var dateTime2 = new GlideDateTime(); // Retrieves the current date
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // sys_id of my schedule

var inSchedule1 = schedule.isInSchedule(dateTime1); // Returns true or false
var inSchedule2 = schedule.isInSchedule(dateTime2); // Returns true or false

gs.print(inSchedule1 + dateTime1);
gs.print(inSchedule2 + dateTime2);

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