How to calculate United States holiday observances

Usually around a holiday I will do a post that’s somehow related to the holiday.

Seemed kind of silly to do a post on how to calculate Independence Day in the
United States since it’s pretty straight forward.

It always lands on the 4th day of the month of July. Hence why we refer to it
as the Fourth of July 😉

With that being said, the Fourth of July is a federal holiday here in the United
States. These type of holidays result in bank closings as well as drunken
fireworks related mishaps.

There are times when Independence Day doesn’t land on a weekday and even though
we still celebrate the holiday on the day it happens, banks (and some employers)
will recognize an additional day as an observance of the holiday.

Seems we’ll do just about anything to get a day off from work. Including but not
limited to observing holidays that land on non-working days on a working day.

The rules are pretty simple:

  • If the holiday lands on a Saturday, observance is on the previous Friday.
  • If the holiday lands on a Sunday, observance is on the following Monday.

This simple set of rules holds true for most holidays here in the United States.

As I’ve learned from my time building [HolidayAPI][hapi] this isn’t always the
case in other countries.

Some countries have their own observance rules that they apply to all holidays.
Some have rules that apply to some holidays and not others. And then there’s
countries that celebrate Christmas and Boxing day and sometimes their
observances fall on the same day and then you have to push things out another
days and…

Don’t even get me started on holidays that are based on lunar cycles and not the
Gregorian calendar!!

Okay, so the rules are simple, here’s how we’d tackle this task in my two
favorite languages, JavaScript and PHP:

JavaScript

let holiday
let dayOfWeek
let observance

holiday = new Date('2018-07-04' // Wednesday, so observance is same day
// holiday = new Date('2015-07-04'); // Saturday, observe on Friday
// holiday = new Date('2010-07-04'); // Observe, observe on Monday

dayOfWeek = holiday.getDay

if (dayOfWeek === 5) {
  holiday.setDate(holiday.getDate() - 1
} else if (dayOfWeek === 6) {
  holiday.setDate(holiday.getDate() + 1
}

observance = holiday.toISOString().slice(0, 10
console.log(observance

PHP

<?php
$holiday = '2018-07-04'
// $holiday = '2015-07-04'; // Saturday, observe on Friday
// $holiday = '2010-07-04'; // Observe, observe on Monday

$day_of_week = date('w', strtotime($holiday
$observance = $holiday

if ($day_of_week == 5) {
    $observance = date('Y-m-d', strtotime($holiday . ' -1 day'
} elseif ($day_of_week == 6) {
    $observance = date('Y-m-d', strtotime($holiday . ' +1 day'
}

echo $observance
Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.