US Federal Holidays (C#)
A DateTime extension method to determine if a given DateTime is one of the ten US federal holidays, using C#.
Introduction
I am unfortunate enough to have to be tasked with writing QlikView scripts instead of being a real programmer, but every once in a while, I get to write some good old-fashioned C# code. The impetus for writing this code is to enhance my existing DateTime extention class, and is basicially a copy of the same function I wrote for SQL (for use in Qlikview scripts).
The SQL version of this tip is here: US Federal Holidays (SQL)
Background
Frequently, the analysts I work with need to know if a particular date is a US federal holiday. This extension method covers all ten of the current federal holidays that are observed as of this writing.
Using the code
Normally, I abhore multiple return points in a method, because I think it muddies the code up. However, my I overrode my OCD tendancies because I felt it was more important to return as soon as possible if a holiday was detected.
There are essentially two types of holidays - holidays that fall on an nth day of the week (such as MLK day, President's Day, etc), and holidays that occur on a certain day of the month but that are adjusted when that day occurs on a weekend (such as New Year's Day, Christmas, etc). For no other reason than "it just made sense to do it that way", I check for each holiday in the order it appears in the year. Using the method below as a template, you can add additional holidays that are applicable to your project(s). Comments indicate which holiday I'm checking for.
/// <summary>
/// Determines if this date is a federal holiday.
/// </summary>
/// <param name="date">This date</param>
/// <returns>True if this date is a federal holiday</returns>
public static bool IsFederalHoliday(this DateTime date)
{
// to ease typing
int nthWeekDay = (int)(Math.Ceiling((double)date.Day / 7.0d));
DayOfWeek dayName = date.DayOfWeek;
bool isThursday = dayName == DayOfWeek.Thursday;
bool isFriday = dayName == DayOfWeek.Friday;
bool isMonday = dayName == DayOfWeek.Monday;
bool isWeekend = dayName == DayOfWeek.Saturday || dayName == DayOfWeek.Sunday;
// New Years Day (Jan 1, or preceding Friday/following Monday if weekend)
if ((date.Month == 12 && date.Day == 31 && isFriday) ||
(date.Month == 1 && date.Day == 1 && !isWeekend) ||
(date.Month == 1 && date.Day == 2 && isMonday)) return true;
// MLK day (3rd monday in January)
if (date.Month == 1 && isMonday && nthWeekDay == 3) return true;
// President’s Day (3rd Monday in February)
if (date.Month == 2 && isMonday && nthWeekDay == 3) return true;
// Memorial Day (Last Monday in May)
if (date.Month == 5 && isMonday && date.AddDays(7).Month == 6) return true;
// Independence Day (July 4, or preceding Friday/following Monday if weekend)
if ((date.Month == 7 && date.Day == 3 && isFriday) ||
(date.Month == 7 && date.Day == 4 && !isWeekend) ||
(date.Month == 7 && date.Day == 5 && isMonday)) return true;
// Labor Day (1st Monday in September)
if (date.Month == 9 && isMonday && nthWeekDay == 1) return true;
// Columbus Day (2nd Monday in October)
if (date.Month == 10 && isMonday && nthWeekDay == 2) return true;
// Veteran’s Day (November 11, or preceding Friday/following Monday if weekend))
if ((date.Month == 11 && date.Day == 10 && isFriday) ||
(date.Month == 11 && date.Day == 11 && !isWeekend) ||
(date.Month == 11 && date.Day == 12 && isMonday)) return true;
// Thanksgiving Day (4th Thursday in November)
if (date.Month == 11 && isThursday && nthWeekDay == 4) return true;
// Christmas Day (December 25, or preceding Friday/following Monday if weekend))
if ((date.Month == 12 && date.Day == 24 && isFriday) ||
(date.Month == 12 && date.Day == 25 && !isWeekend) ||
(date.Month == 12 && date.Day == 26 && isMonday)) return true;
return false;
}
Points of Interest
It's not hard to prepare bacon. And it's yummy.
History
01 Feb 2017 - Original submission.