Why " isInSchedule() " always entering Else part
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2024 09:32 AM - edited 04-12-2024 11:08 AM
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 '
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 07:55 AM - edited 04-17-2024 03:01 PM
I suggest just using date/time in UTC, as the platform uses that to present date/time in the system Time Zone, or user's time zone.
var now = new GlideDateTime();
gs.info("checking date/time: " + now);
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // 8-5 weekdays excluding holidays
if (schedule.isInSchedule(now)) {
gs.info('It is in Schedule');
} else {
gs.info('It is Not in Schedule');
}
Using local time seems to fail.
[Update] the previous script, using a local date/time failed earlier. However, it now works. But more testing is needed, once current UTC time is outside of the schedule. Using the following to test:
var now = new GlideDateTime();
gs.info("checking date/time: " + now + " utc");
var localDate = now.getLocalDate().toString();
var localTime = now.getLocalTime().toString();
var nowString = localDate + " " + localTime.substring(11);
var nowValue = new GlideDateTime(nowString);
gs.info("local date/time string = " + nowString + ", value = " + nowValue);
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // 8-5 weekdays excluding holidays
if (schedule.isInSchedule(nowValue)) {
gs.info('Value is in Schedule');
} else {
gs.info('Value is Not in Schedule');
}
if (schedule.isInSchedule(now)) {
gs.info('now (utc) is in Schedule');
} else {
gs.info('now (utc) is Not in Schedule');
}
// let's try another API for current date/time
var gs_now = gs.nowDateTime();
var gsNow = new GlideDateTime(gs_now);
gs.info("checking gs.nowDateTime() date/time: " + gsNow);
if (schedule.isInSchedule(gsNow)) {
gs.info('gs.nowDateTime() is in Schedule');
} else {
gs.info('gs.nowDateTime() is Not in Schedule');
}
// Let's try yet another API for current date/time
var gs_glide_now = gs.nowGlideDateTime();
var gsNow = new GlideDateTime(gs_glide_now);
gs.info("checking nowGlideDateTime() date/time: " + gsNow);
if (schedule.isInSchedule(gsNow)) {
gs.info('gs.nowGlideDateTime() is in Schedule');
} else {
gs.info('gs.nowGlideDateTime() now() is Not in Schedule');
}
results in:
*** Script: checking date/time: 2024-04-17 17:11:43 utc
*** Script: local date/time string = 2024-04-17 13:11:43, value = 2024-04-17 13:11:43
*** Script: Value is in Schedule
*** Script: now (utc) is in Schedule
*** Script: checking gs.nowDateTime() date/time: 2024-04-17 13:11:43
*** Script: gs.nowDateTime() is in Schedule
*** Script: checking nowGlideDateTime() date/time: 2024-04-17 17:11:43
*** Script: gs.nowGlideDateTime() is in Schedule
Your milage may vary. I tested before 17:00:00 UTC and after 17:00:00 UTC and got the same results shown.
[Update: running after 5pm on a weekday]
Results show:
*** Script: checking date/time: 2024-04-17 21:51:28 utc
*** Script: local date/time string = 2024-04-17 17:51:28, value = 2024-04-17 17:51:28
*** Script: Value is in Schedule
*** Script: now (utc) is Not in Schedule
*** Script: checking gs.nowDateTime() date/time: 2024-04-17 17:51:28
*** Script: gs.nowDateTime() is in Schedule
*** Script: checking nowGlideDateTime() date/time: 2024-04-17 21:51:28
*** Script: gs.nowGlideDateTime() now() is Not in Schedule
So seems the best approach is the following:
var now = new GlideDateTime();
gs.info("checking date/time: " + now + " utc");
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // 8-5 weekdays excluding holidays
if (schedule.isInSchedule(now)) {
gs.info('now (utc) is in Schedule');
} else {
gs.info('now (utc) is Not in Schedule');
}
will work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 11:23 AM - edited 04-18-2024 12:12 PM
gs.nowDateTime(); is not supporting in Scoped Application.
any suggestions please
instance is US/Eastern
I tried below script
schedule
090eecae0a0a0b260077e1dfa71da828
US/Eastern
08:00:00 to 17:00:00
until : Empty
var nowDT = new GlideDateTime(new GlideDateTime().getDisplayValue()); gs.addInfoMessage(nowDT); var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); if(schedule.isInSchedule(nowDT)){ gs.addInfoMessage("isInSchedule"); }else{ gs.addInfoMessage("Not isInSchedule"); } Result : 2024-04-18 15:44:09 isInSchedule
2024-04-18 19:49:19 is Out of schedule, but still it is showing "isInSchedule"
var nowDT = new GlideDateTime(new GlideDateTime("2024-04-18 19:49:19")); gs.addInfoMessage(nowDT); var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); if(schedule.isInSchedule(nowDT)){ gs.addInfoMessage("isInSchedule"); }else{ gs.addInfoMessage("Not isInSchedule"); } Result : 2024-04-18 19:49:19 isInSchedule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2024 11:34 AM
I ran:
var now = new GlideDateTime();
gs.info("checking date/time: " + now + " utc");
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828"); // 8-5 weekdays excluding holidays
if (schedule.isInSchedule(now)) {
gs.info('now is in Schedule');
} else {
gs.info('now is Not in Schedule');
}
In scripts background in Global and two scoped applications.
Seems you are going to have to post more details.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 06:38 AM
I understand that we are doing it complicated, but not sure how to fix this one.
Kindly let me know here what kind of details need for you.
my Instance always runs in EST time-zone and my schedules also got created with EST time-zone.
then how come this system always returning UTC value even after using .getDsplayValue();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 08:00 AM - edited 04-19-2024 08:04 AM
getDisplayValue() returns the date-time value in the user's time zone. date-time values are stored in UTC so that the platform can determine/compare values regardless of time zone.
1. Post your code.
2. Where does your code reside? Business Rule, script include, someplace else and what is the Application scope.
3. How are you accessing/running the code, based on where it resides?
and
For ServerScope usage.