The CreatorCon Call for Content is officially open! Get started here.

How to check if a day is a U.S. Holiday in Script Include

AM24
Giga Guru

I want to check if a certain day is a U.S. holiday. I have a script include for this. No matter what, this always returns false. The schedule I am using is the schedule for U.S. holidays. I call this script include from a scheduled job. I tried adding time zone in the isInSchedule method as well as US\Eastern, but that did not work either. I was wondering how I could get this to work. Thanks!

find_real_file.png

3 REPLIES 3

sachin_namjoshi
Kilo Patron
Kilo Patron

You can use below code to check if day is a US holiday based on schedule.

 

var g = new GlideRecord('cmn_schedule');
g.addQuery('name', '8-5 weekdays excluding holidays');
g.query();
if (g.next()) {
  var sched = new GlideSchedule(g.sys_id);
  var d = new GlideDateTime();
  d.setDisplayValue("2020-12-25 12:00:00");
  if(sched.isInSchedule(d))
    gs.info("not a holiday");
  else
    gs.info("this is a holiday");
}

 

Regards,

Sachin

DrewW
Mega Sage

Go to the Schedule record and click the "Show Schedule" Related Link and see what it looks like.

Its an issue with the schedule.  I see that if I run @sachin.namjoshi code using the "U.S. Holidays" schedule it does not work but if I use the 8-5 one it does.  So I think all you need to do is create a schedule that shows 24x7 free and then add the "U.S. Holidays" as a child schedule with the type of "Include" then use that schedule and it will work fine.

You may need to revers your if statement logic.

 

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi AM,

It's explained in the following Now Support page.

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0792369

Scheduled Entries under U.S. Holidays have type "Excluded" so those dates are excluded.

To use U.S. Holidays as it, it's necessary to use them as a child Schedule.

To use U.S. Holidays directly, it's necessary to change the "Type" of Scheduld Entries to "None".

find_real_file.png

Execution result:

Script:

var g = new GlideRecord('cmn_schedule');
if (g.get('name', 'U.S. Holidays')) {
  var schedule = new GlideSchedule(g.sys_id);
  var gdt = new GlideDateTime();
  gdt.setDisplayValue('2021-12-25 10:00:00');
  if (schedule.isInSchedule(gdt)) {
    gs.info('Is holiday');
  } else {
    gs.info('Not holiday');
  }
} 

Result:

*** Script: Is holiday