Why " isInSchedule() " always entering Else part

Supriya25
Tera Guru

Hi All,

I tried below , but it's always entering into ELSE Part.

It's returning UTC time value but I'm looking at EST timezone value validation.

var now= new GlideDateTime();

var nowDate = new GlideDateTime(now.getDisplayValueInternal());
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); 8-5 working days sys-id I took
if (schedule.isInSchedule(nowDate)) {
gs.info('It is in Schedule');
}else{

gs.info('It is Not in Schedule ');

}

always returning ' It is Not in Schedule  ' 

14 REPLIES 14

Maddysunil
Kilo Sage

@Supriya25 

  1. Timezone Issue: ServiceNow stores dates and times in UTC by default. If you're trying to compare the current time with a schedule that's defined in a different timezone (e.g., EST), you need to adjust the current time to that timezone. You can do this by setting the timezone of the GlideDateTime object.

  2. Schedule Configuration: Ensure that the schedule you're referencing ("090eecae0a0a0b260077e1dfa71da828") is correctly configured to represent the working hours in the EST timezone. Double-check the schedule definition to ensure it aligns with your expectations.

 

var now = new GlideDateTime();
// Set the timezone of the current time to EST
now.setTZ('America/New_York');

var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // Assuming this is correctly configured for EST
if (schedule.isInSchedule(now)) {
    gs.info('It is in Schedule');
} else {
    gs.info('It is Not in Schedule');
}

 

Make sure to replace 'America/New_York' with the appropriate timezone identifier for EST if it's different in your ServiceNow instance. Additionally, ensure that the schedule ID "090eecae0a0a0b260077e1dfa71da828" corresponds to the correct schedule you want to check against.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

unfortunately system not allowing  in SCOPED application. any solution for this.

setTZ

 

@Supriya25 

One alternative approach you can try is to adjust the current time manually to the desired timezone offset. For example, to convert UTC to Eastern Standard Time (EST), which is UTC-5, we can subtract 5 hours from the UTC time.

 

// Get the current time in the UTC timezone
var nowUTC = new GlideDateTime();

// Adjust the current time to Eastern Standard Time (UTC - 5 hours)
var nowEST = new GlideDateTime();
nowEST.setValue(nowUTC.getValue());
nowEST.addSeconds(-5 * 60 * 60); // Subtract 5 hours

// Create a GlideSchedule object using the schedule ID
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828");

// Check if the adjusted time falls within the schedule
if (schedule.isInSchedule(nowEST)) {
    gs.info('It is in Schedule');
} else {
    gs.info('It is Not in Schedule');
}

 

  

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

That doesn't account for Daylight Savings time, currently EDT has a 4 hour offset from UTC.

 

GlideDateTime API-getLocalDate

 

Review 'getLocalTime' there, parse the time portion.

 

 

var now = new GlideDateTime();
gs.info("checking date/time: " + now);
var localDate = now.getLocalDate().toString();
var localTime = now.getLocalTime().toString();
var nowString = localDate + " " + localTime.substring(11);
gs.info("local date/time = " + nowString);
var nowValue = new GlideDateTime(nowString);

var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // 8-5 weekdays excluding holidays
if (schedule.isInSchedule(nowValue)) {
    gs.info('It is in Schedule');
} else {
    gs.info('It is Not in Schedule');
}

 

Results in:

 

 

*** Script: checking date/time: 2024-04-16 16:34:05
*** Script: local date/time = 2024-04-16 12:34:05
*** Script: It is in Schedule

 

 

And when run on a date/time that is in the "8 -5 weekdays excluding holidays" schedule it works even though the value returned from line 1 is UTC.

Let me try this solution