Click here to Skip to main content
15,881,173 members
Articles / Programming Languages / C#
Tip/Trick

US Federal Holidays (C#)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (13 votes)
31 Jan 2017CPOL1 min read 27.3K   8   9
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.

C#
/// <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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
QuestionJuneteenth Pin
#realJSOP26-Mar-24 7:44
mve#realJSOP26-Mar-24 7:44 
QuestionPerformance Pin
mac_v3-Feb-17 5:29
professionalmac_v3-Feb-17 5:29 
AnswerRe: Performance Pin
#realJSOP26-Mar-24 7:45
mve#realJSOP26-Mar-24 7:45 
QuestionSuggestion for Christmas on a Sunday Pin
Mike Meinz2-Feb-17 6:55
Mike Meinz2-Feb-17 6:55 
AnswerRe: Suggestion for Christmas on a Sunday Pin
#realJSOP2-Feb-17 6:59
mve#realJSOP2-Feb-17 6:59 
QuestionTiny tiny little thing Pin
bobrien1001-Feb-17 9:23
bobrien1001-Feb-17 9:23 
AnswerRe: Tiny tiny little thing Pin
bling1-Feb-17 11:38
bling1-Feb-17 11:38 
AnswerRe: Tiny tiny little thing Pin
#realJSOP2-Feb-17 7:07
mve#realJSOP2-Feb-17 7:07 
QuestionNot bad... Pin
Pete O'Hanlon1-Feb-17 1:28
mvePete O'Hanlon1-Feb-17 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.