17

I need to know how to calculate USA holidays. I need a solution that works for any year. I didn't want to simply store the dates in a database that would need to be maintained.

For holidays on the weekend it needs to follow policy of the US government to adjust to a weekday. If it falls on a Saturday it is adjusted to Friday. If it falls on a Sunday it needs to be adjusted to Monday. I understand many (most?) banks in the US do it the same way.

How do I calculate a list of USA holidays?

Public Function getHolidayList(ByVal vYear As Integer) As List(Of Date)

    Dim HolidayList As New List(Of Date)

    '...fill the list with holidays 
    ' New Year's Day            Jan 1
    ' Martin Luther King, Jr. third Mon in Jan
    ' Washington's Birthday third Mon in Feb
    ' Memorial Day          last Mon in May
    ' Juneteenth            June 19th
    ' Independence Day      July 4
    ' Labor Day             first Mon in Sept
    ' Columbus Day          second Mon in Oct
    ' Veterans Day          Nov 11
    ' Thanksgiving Day      fourth Thur in Nov
    ' Christmas Day         Dec 25

    'adjust for weekends

End Function

www.archives.gov

www.usa.gov

www.opm.gov

8
  • I can write the code, but it's the reference I'm interested in. I'll trust that these are all correct, but citations would make me more confident. This is the first useful hit I've found in my websearch... I'm surprised I haven't found a .gov list that does more than list them for each year. Oct 4, 2016 at 21:02
  • @MichaelScheper see links in q above.
    – D_Bester
    Oct 6, 2016 at 13:32
  • @MichaelScheper the links don't actually explain how they determine the date; Just shows the date. I had to research each holiday for that.
    – D_Bester
    Oct 6, 2016 at 13:35
  • @MichaelScheper as to the adjustment for weekends: I don't have a citation for that. I think I read it in the tax manuals. Years of experience proves it is true that Sat>>Fri and Sun>>Mon.
    – D_Bester
    Oct 6, 2016 at 13:40
  • Yeah, it was the date determination I was looking for, so I can blame somebody else if my code is wrong. 😉 Do those weekend adjustments apply to all holidays, or only some? I seem to recall missing out on a US holiday at one point, because it landed on a weekend. (And FWIW, while most Australian holidays are weekend-adjusted, not all are—Anzac Day, for example, is always observed on 25 April, regardless of day of the week.) Oct 7, 2016 at 15:25

10 Answers 10

21

This is one way to do it. A weakness of this method is that since the rules are hard coded I would need to change the code in the rare event that congress changed the rules. For my in-house software that is not a problem but it might be for others.

Also I don't calculate Easter since that is not a US federal holiday. See Nature (1876) Algorithm for Calculating the Date of Easter

Public Function getHolidayList(ByVal vYear As Integer) As List(Of Date)

    Dim FirstWeek As Integer = 1
    Dim SecondWeek As Integer = 2
    Dim ThirdWeek As Integer = 3
    Dim FourthWeek As Integer = 4
    Dim LastWeek As Integer = 5

    Dim HolidayList As New List(Of Date)

    '   http://www.usa.gov/citizens/holidays.shtml      
    '   http://archive.opm.gov/operating_status_schedules/fedhol/2013.asp

    ' New Year's Day            Jan 1
    HolidayList.Add(DateSerial(vYear, 1, 1))

    ' Martin Luther King, Jr. third Mon in Jan
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 1, 1), DayOfWeek.Monday, ThirdWeek))

    ' Washington's Birthday third Mon in Feb
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 2, 1), DayOfWeek.Monday, ThirdWeek))

    ' Memorial Day          last Mon in May
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 5, 1), DayOfWeek.Monday, LastWeek))

    ' Juneteenth National Independence Day, June 19
    HolidayList.Add(DateSerial(vYear, 6, 19))

    ' Independence Day      July 4
    HolidayList.Add(DateSerial(vYear, 7, 4))

    ' Labor Day             first Mon in Sept
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 9, 1), DayOfWeek.Monday, FirstWeek))

    ' Columbus Day          second Mon in Oct
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 10, 1), DayOfWeek.Monday, SecondWeek))

    ' Veterans Day          Nov 11
    HolidayList.Add(DateSerial(vYear, 11, 11))

    ' Thanksgiving Day      fourth Thur in Nov
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 11, 1), DayOfWeek.Thursday, FourthWeek))

    ' Christmas Day         Dec 25
    HolidayList.Add(DateSerial(vYear, 12, 25))

    'saturday holidays are moved to Fri; Sun to Mon
    For i As Integer = 0 To HolidayList.Count - 1
        Dim dt As Date = HolidayList(i)
        If dt.DayOfWeek = DayOfWeek.Saturday Then
            HolidayList(i) = dt.AddDays(-1)
        End If
        If dt.DayOfWeek = DayOfWeek.Sunday Then
            HolidayList(i) = dt.AddDays(1)
        End If
    Next

    'return
    Return HolidayList

End Function

Private Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
    'specify which day of which week of a month and this function will get the date
    'this function uses the month and year of the date provided

    'get first day of the given date
    Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)

    'get first DayOfWeek of the month
    Dim dtRet As Date = dtFirst.AddDays(6 - dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek)

    'get which week
    dtRet = dtRet.AddDays((WhichWeek - 1) * 7)

    'if day is past end of month then adjust backwards a week
    If dtRet >= dtFirst.AddMonths(1) Then
        dtRet = dtRet.AddDays(-7)
    End If

    'return
    Return dtRet

End Function
1
  • 1
    This gives inaccurate results for 2020 when Juneteenth was not observed. Also if you are consuming this algorithm, make sure that you consider how New Year's Day 2022 will be treated, as it occurs on Saturday and is observed in the prior year. If you're assuming this function will return all holidays that were observed in 2021, 12/31/2021 should have been included in the list. If you're assuming it will return all holidays that occurred in 2021, it should not have been included.
    – Rich Moss
    Oct 28, 2022 at 18:45
12

Converted to C# as a console program... I needed the dates in a text file quickly:

class Program
{
    public class Holiday
    {
        public string HolidayName { get; set; }
        public DateTime Date { get; set; }

        public Holiday(string holidayName, DateTime date)
        {
            HolidayName = holidayName;
            Date = date;
        }
    }

    public static List<Holiday> getHolidayList(int vYear)
    {
        int FirstWeek = 1;
        int SecondWeek = 2;
        int ThirdWeek = 3;
        int FourthWeek = 4;
        int LastWeek = 5;

        List<Holiday> HolidayList = new List<Holiday>();

        //   http://www.usa.gov/citizens/holidays.shtml      
        //   http://archive.opm.gov/operating_status_schedules/fedhol/2013.asp

        // New Year's Day            Jan 1
        HolidayList.Add(new Holiday("NewYears", new DateTime(vYear, 1, 1)));

        // Martin Luther King, Jr. third Mon in Jan
        HolidayList.Add(new Holiday("MLK", GetNthDayOfNthWeek(new DateTime(vYear, 1, 1), DayOfWeek.Monday, ThirdWeek)));

        // Washington's Birthday third Mon in Feb
        HolidayList.Add(new Holiday("WashingtonsBDay", GetNthDayOfNthWeek(new DateTime(vYear, 2, 1), DayOfWeek.Monday, ThirdWeek)));

        // Memorial Day          last Mon in May
        HolidayList.Add(new Holiday("MemorialDay", GetNthDayOfNthWeek(new DateTime(vYear, 5, 1), DayOfWeek.Monday, LastWeek)));

        // Independence Day      July 4
        HolidayList.Add(new Holiday("IndependenceDay", new DateTime(vYear, 7, 4)));

        // Labor Day             first Mon in Sept
        HolidayList.Add(new Holiday("LaborDay", GetNthDayOfNthWeek(new DateTime(vYear, 9, 1), DayOfWeek.Monday, FirstWeek)));

        // Columbus Day          second Mon in Oct
        HolidayList.Add(new Holiday("Columbus", GetNthDayOfNthWeek(new DateTime(vYear, 10, 1), DayOfWeek.Monday, SecondWeek)));

        // Veterans Day          Nov 11
        HolidayList.Add(new Holiday("Veterans", new DateTime(vYear, 11, 11)));

        // Thanksgiving Day      fourth Thur in Nov
        HolidayList.Add(new Holiday("Thanksgiving", GetNthDayOfNthWeek(new DateTime(vYear, 11, 1), DayOfWeek.Thursday, FourthWeek)));

        // Christmas Day         Dec 25
        HolidayList.Add(new Holiday("Christmas", new DateTime(vYear, 12, 25)));

        //saturday holidays are moved to Fri; Sun to Mon
        foreach (var holiday in HolidayList)
        {
            if (holiday.Date.DayOfWeek == DayOfWeek.Saturday)
            {
                holiday.Date = holiday.Date.AddDays(-1);
            }
            if (holiday.Date.DayOfWeek == DayOfWeek.Sunday)
            {
                holiday.Date = holiday.Date.AddDays(1);
            }
        }

        //return
        return HolidayList;

    }

    private static System.DateTime GetNthDayOfNthWeek(DateTime dt, DayOfWeek dayofWeek, int WhichWeek)
    {
        //specify which day of which week of a month and this function will get the date
        //this function uses the month and year of the date provided

        //get first day of the given date
        System.DateTime dtFirst = new DateTime(dt.Year, dt.Month, 1);

        //get first DayOfWeek of the month
        System.DateTime dtRet = dtFirst.AddDays(6 - (int)dtFirst.AddDays(-1 * ((int)dayofWeek + 1)).DayOfWeek);

        //get which week
        dtRet = dtRet.AddDays((WhichWeek - 1) * 7);

        //if day is past end of month then adjust backwards a week
        if (dtRet >= dtFirst.AddMonths(1))
        {
            dtRet = dtRet.AddDays(-7);
        }

        //return
        return dtRet;

    }

    static void Main(string[] args)
    {
        for(int ii = 2013; ii < 2100; ii++)
        {
            var holidays = getHolidayList(ii);

            foreach (var holiday in holidays)
            {
                Console.WriteLine(holiday.HolidayName + ": " + holiday.Date.ToShortDateString());
            }
        }
    }
}
2
  • Your missing moving Saturday/Sunday holidays to Friday/Monday
    – Sevin7
    Jul 17, 2015 at 15:50
  • To calculate dtRet. To me an easier to understand version of that is: DateTime dtRet = dtFirst.AddDays(((int)dayofWeek + 7 - (int)dtFirst.DayOfWeek) % 7);
    – dko
    Feb 24, 2017 at 17:03
6

For reference, the list of Federal Holidays is defined in 5 U.S. Code § 6103. Holidays:

  • New Year’s Day, January 1.
  • Birthday of Martin Luther King, Jr., the third Monday in January.
  • Washington’s Birthday, the third Monday in February.
  • Memorial Day, the last Monday in May.
  • Juneteenth National Independence Day, June 19. (Newly added as of 2021)
  • Independence Day, July 4.
  • Labor Day, the first Monday in September.
  • Columbus Day, the second Monday in October.
  • Veterans Day, November 11.
  • Thanksgiving Day, the fourth Thursday in November.
  • Christmas Day, December 25.

For regular Monday through Friday workers, holidays that land on a Saturday will be observed on the Friday before. I've read in other places that holidays landing on a Sunday will be observed the following Monday.

EDIT: If you are implementing an algorithm for observed holiday dates from the paragraph above, you should test 1/1/2022 since it occurs on a Saturday and should be observed the Friday before, in the prior year.

2

Jay Muntz on CodeProject has an excellent implementation of holidays using an XML file to store the rules. That code doesn't adjust for weekends though.

Also his version of tax day (April 15) doesn't correct for Emancipation Day in Washington DC although that may be irrelevant for this question.

Dynamic Holiday Date Calculator By Jay Muntz, 5 Jan 2006

2

Here's some code I whipped up in C#. Each holiday is represented as an instance of Holiday. To get the date of observance by the federal government, simply call the object's FederalObservance method. Notice, however, that only one of the holidays is an actual holiday; I just created two holidays as examples.

        public static void Main (string[] args)
    {
        Holiday SatHoliday = new Holiday ("Satman", 2015, 11, 20);
        Holiday Thanksgiving = new Holiday ("Thanksgiving", 2015, 11, 3, DayOfWeek.Thursday);
        Holiday[] holidays = new Holiday[] { SatHoliday, Thanksgiving };

    }
}
class Holiday
{
    public DateTime hDate;
    public String name;
    public Holiday(String name, int year, int month, int day)
    {
        this.name = name;
        this.hDate = new DateTime (year, month, day);
    }
        public Holiday(String name, int year, int month, int weekNum/* Weeks are numbered starting with 0. So, the first week would be 0, the second week 1, etc.*/, DayOfWeek dayOfWeek)
    {
        this.name = name;
        DateTime firstOfMonth = new DateTime (year, month, 1);
        this.hDate = firstOfMonth.AddDays (7 * weekNum + (int)dayOfWeek - (int)firstOfMonth.DayOfWeek);
    }
    public DateTime FederalObservance()
    {

                    if (hDate.DayOfWeek == DayOfWeek.Saturday) {
                        return hDate.AddDays (-1);
                    } else if (hDate.DayOfWeek == DayOfWeek.Sunday) {
                        return hDate.AddDays (1);
        } else {
            return hDate;
        }
    }
}
1

Thanks so much for your answer D_Bester. I was trying to do this in VBA for Excel.

I got your VB code translated into VBA for Excel. I thought I would post my code here, for anyone else who may be trying that language.

Sub displayHolidays()
    Dim myHolidayList As New Collection
    Set myHolidayList = GetHolidayList(2005)

    For x = 1 To (myHolidayList.Count)
        Debug.Print myHolidayList(x)
    Next
End Sub

Function GetHolidayList(ByVal vYear As Integer) As Collection
    Const FirstWeek As Integer = 1
    Const SecondWeek As Integer = 2
    Const ThirdWeek As Integer = 3
    Const FourthWeek As Integer = 4
    Const LastWeek As Integer = 5
    Const DayofWeek_Monday As Integer = 2
    Const DayofWeek_Thursday As Integer = 5
    Const DayofWeek_Saturday As Integer = 7
    Const DayofWeek_Sunday As Integer = 1

    Dim holidayList As New Collection
    Dim finalHolidayList As New Collection

    '   http://www.usa.gov/citizens/holidays.shtml
    '   http://archive.opm.gov/operating_status_schedules/fedhol/2013.asp

    ' New Year's Day            Jan 1
    holidayList.Add (DateSerial(vYear, 1, 1))

    ' Martin Luther King, Jr. third Mon in Jan
    holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 1, 1), DayofWeek_Monday, ThirdWeek))

    ' Washington's Birthday third Mon in Feb
    holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 2, 1), DayofWeek_Monday, ThirdWeek))

    ' Memorial Day          last Mon in May
    holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 5, 1), DayofWeek_Monday, LastWeek))

    ' Independence Day      July 4
    holidayList.Add (DateSerial(vYear, 7, 4))

    ' Labor Day             first Mon in Sept
    holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 9, 1), DayofWeek_Monday, FirstWeek))

    ' Columbus Day          second Mon in Oct
    holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 10, 1), DayofWeek_Monday, SecondWeek))

    ' Veterans Day          Nov 11
    holidayList.Add (DateSerial(vYear, 11, 11))

    ' Thanksgiving Day      fourth Thur in Nov
    holidayList.Add (GetNthDayOfNthWeek(DateSerial(vYear, 11, 1), DayofWeek_Thursday, FourthWeek))

    ' Christmas Day         Dec 25
    holidayList.Add (DateSerial(vYear, 12, 25))

    'saturday holidays are moved to Fri; Sun to Mon
    For Each dt In holidayList
        If Weekday(dt) = DayofWeek_Saturday Then
            dt = DateAdd("d", -1, dt)
        End If
        If Weekday(dt) = DayofWeek_Sunday Then
            dt = DateAdd("d", 1, dt)
        End If
        finalHolidayList.Add dt
    Next dt

    'return
    Set GetHolidayList = finalHolidayList
End Function

Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
    'specify which day of which week of a month and this function will get the date
    'this function uses the month and year of the date provided
    Dim dtFirst As Date
    Dim innerDate As Date
    Dim monthAdd As Date

    'get first day of the given date
    dtFirst = DateSerial(Year(dt), Month(dt), 1)

    'get first DayOfWeek of the month
    innerDate = DateAdd("d", -DayofWeek, dtFirst)
    GetNthDayOfNthWeek = DateAdd("d", (7 - Weekday(innerDate)), dtFirst)

    'get which week
    GetNthDayOfNthWeek = DateAdd("d", ((WhichWeek - 1) * 7), GetNthDayOfNthWeek)

    'if day is past end of month then adjust backwards a week
    monthAdd = DateAdd("m", 1, dtFirst)
    If GetNthDayOfNthWeek >= monthAdd Then
        GetNthDayOfNthWeek = DateAdd("d", (-7), GetNthDayOfNthWeek)
    End If
End Function
1

There are edge cases for the Sat>>Fri and Sun>>Mon weekend rules: New Year's Day 2022 is on a Saturday, but my employer does not want to observe it to Dec 31, as the year of the holiday will change (they are creating a 2022 floating holiday instead.)
I had been responsible for an application that supported standing funds transfers on specific days of the month, including the last day. The weekend rule was to push to Monday, and the holiday rule was the next day -- when Aug 31 was a Saturday, both rules applied, and some users objected to the push to Tuesday Sept 3. So a new rule was to fall forward to Friday for last day of the month landing on a three-day weekend. Regarding Easter - the NY Stock Exchange closes for Good Friday, so an Easter calculation is a valid business case.
Sometimes just having an annual entry of holidays is a better user solution.

1
  • Excellent point about New Year's Day 2022. Most algorithms that take year 2021 and return all observed holidays should return 12/31/2021 - same for any other year where 12/31 is on a Friday. Federal observed holidays is an easier problem to solve than company observed holidays, which can be at the whim of the employer.
    – Rich Moss
    Oct 28, 2022 at 18:59
0

Nager.Date supports the public holiday for USA the full source code is available on github a lot of other coutries are supported.

Nuget

PM> install-package Nager.Date

Example:

Get all publicHolidays of a year

var publicHolidays = DateSystem.GetPublicHoliday("US", 2017);

Check if a date a public holiday

if (DateSystem.IsPublicHoliday(date, "US"))
{
    //logic
}
0

Here is a C# solution using Lync. I didn't see any other answers using this approach. I'm putting all the days of the month into a List of type DateTime and then using Lync to select exactly what I want. Yes this takes up more time/memory, but if that's not the most critical priority, look how easily it reads...

/// <summary>
/// Find the Nth day of the month that matches the given dayOfWeek
/// </summary>
/// <param name="year">The year</param>
/// <param name="month">The month number 1 - 12</param>
/// <param name="dayOfWeek">The day of the week</param>
/// <param name="occurance">Take the Nth day found</param>
/// <returns></returns>
public static DateTime GetNthDayOfMonth(int year, int month, DayOfWeek dayOfWeek, int occurance)
{
    return GetDaysInMonth(year, month).Where(d => d.DayOfWeek == dayOfWeek).Skip(occurance - 1).FirstOrDefault();
}

/// <summary>
/// Find the Nth last day of the month that matches the given dayOfWeek
/// </summary>
/// <param name="year">The year</param>
/// <param name="month">The month number 1 - 12</param>
/// <param name="dayOfWeek">The day of the week</param>
/// <param name="occurance">Take the Nth day found</param>
/// <returns></returns>
public static DateTime GetNthLastDayOfMonth(int year, int month, DayOfWeek dayOfWeek, int occurance)
{
    return GetDaysInMonth(year, month).Where(d => d.DayOfWeek == dayOfWeek).Reverse().Skip(occurance - 1).FirstOrDefault();
}

/// <summary>
/// Put all dates of the month into a list that lync can work with
/// </summary>
/// <param name="year">The year</param>
/// <param name="month">The month number 1 - 12</param>
/// <returns></returns>
public static List<DateTime> GetDaysInMonth(int year, int month)
{
    List<DateTime> days = new List<DateTime>();
    var d = new DateTime(year, month, 1);
    while(d.Month == month)
    {
        days.Add(d);
        d = d.AddDays(1);
    }
    return days;
}
0

I adapted the excellent holiday formula list from https://www.extendoffice.com/documents/excel/2602-excel-if-date-is-public-holiday.html

Holiday Item Cell Formulas Nearest Weekday
Current Year C2 =YEAR(NOW())
New Years Day C3 =DATE(C2,1,1) =C3+CHOOSE(WEEKDAY(C3),1,0,0,0,0,0,-1)
Martin Luther King Jr. Day C4 =DATE(C2,1,1)+14+CHOOSE(WEEKDAY(DATE(C2,1,1)),1,0,6,5,4,3,2)
Presidents Day C5 =DATE(C2,2,1)+14+CHOOSE(WEEKDAY(DATE(C2,2,1)),1,0,6,5,4,3,2)
Good Friday C6 =C7-2
Easter Sunday C7 =DATE(C2,3,1)+((MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) - INT(MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) / 28) * (1 - INT(MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) / 28) * INT(29 / (MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) + 1)) * INT((21 - MOD(C2, 19)) / 11))) - (MOD(C2 + INT(C2 / 4) + (MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) - INT(MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) / 28) * (1 - INT(MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) / 28) * INT(29 / (MOD(INT(C2 / 100) - INT(INT(C2 / 100) / 4) - INT((8 * INT(C2 / 100) + 13) / 25) + 19 * MOD(C2, 19) + 15, 30) + 1)) * INT((21 - MOD(C2, 19)) / 11))) + 2 - INT(C2 / 100) + INT(INT(C2 / 100) / 4), 7)) + 27)
Memorial Day C8 =DATE(C2,6,1)-WEEKDAY(DATE(C2,6,6))
Juneteenth C9 =DATE(C2,6,19) =C9+CHOOSE(WEEKDAY(C9),1,0,0,0,0,0,-1)
Independence Day C10 =DATE(C2,7,4) =C10+CHOOSE(WEEKDAY(C10),1,0,0,0,0,0,-1)
Labor Day C11 =DATE(C2,9,1)+CHOOSE(WEEKDAY(DATE(C2,9,1)),1,0,6,5,4,3,2)
Columbus Day C12 =DATE(C2,10,1)+7+CHOOSE(WEEKDAY(DATE(C2,10,1)),1,0,6,5,4,3,2)
Veterans Day C13 =DATE(C2,11,11) =C13+CHOOSE(WEEKDAY(C13),1,0,0,0,0,0,-1)
Thanksgiving C14 =DATE(C2,11,1)+21+CHOOSE(WEEKDAY(DATE(C2,11,1)),4,3,2,1,0,6,5)
Christmas Day C15 =DATE(C2,12,25) =C15+CHOOSE(WEEKDAY(C15),1,0,0,0,0,0,-1)

Once you paste the table above into Excel, you can replace the Cell addresses in column B with the formula ="C"&ROW()

In C# or VB, you can change the initial DATE(...)+ to DATE(...).AddDays(...) and implement the Excel functions something like this:

    // C# Version
    DateTime DATE(int year, int month, int day) { return new DateTime(year, month, day); }
    int WEEKDAY(DateTime date) { return 1 + (int)date.DayOfWeek; }
    int CHOOSE(params int[] list) { return list[0] < list.Length ? list[list[0]] : 0; }
    int MOD(int value, int modulus) { return value % modulus; }
    int INT(float value) { return (int)value; }
    public int EasterOffset(int year)
    {
        var offset = ((MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) - INT(MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) / 28) * (1 - INT(MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) / 28) * INT(29 / (MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) + 1)) * INT((21 - MOD(year, 19)) / 11))) - (MOD(year + INT(year / 4) +
            (MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) - INT(MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) / 28) * (1 - INT(MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) / 28) * INT(29 / (MOD(INT(year / 100) - INT(INT(year / 100) / 4) - INT((8 * INT(year / 100) + 13) / 25)
            + 19 * MOD(year, 19) + 15, 30) + 1)) * INT((21 - MOD(year, 19)) / 11))) + 2 - INT(year / 100) + INT(INT(year / 100) / 4), 7)) + 27);
        return offset;
    }

    public Dictionary<string, DateTime> CalculateHolidays(int year = 0)
    {
        var calc = new Dictionary<string, DateTime>();
        year = year > 0 ? year : DateTime.Today.Year;
        calc.Add("New Years Day", DATE(year, 1, 1));
        calc.Add("Martin Luther King Jr. Day", DATE(year, 1, 1).AddDays(14 + CHOOSE(WEEKDAY(DATE(year, 1, 1)), 1, 0, 6, 5, 4, 3, 2)));
        calc.Add("President Day", DATE(year, 2, 1).AddDays(14 + CHOOSE(WEEKDAY(DATE(year, 2, 1)), 1, 0, 6, 5, 4, 3, 2)));
        calc.Add("Good Friday", DATE(year, 3, 1).AddDays(EasterOffset(year) - 2));
        calc.Add("Easter Sunday", DATE(year, 3, 1).AddDays(EasterOffset(year)));
        calc.Add("Memorial Day", DATE(year, 6, 1).AddDays(-WEEKDAY(DATE(year, 6, 6))));
        calc.Add("Juneteenth", DATE(year, 6, 19));
        calc.Add("Independence Day", DATE(year, 1, 1));
        calc.Add("Labor Day", DATE(year, 7, 4));
        calc.Add("Columbus Day", DATE(year, 10, 1).AddDays(7 + CHOOSE(WEEKDAY(DATE(year, 10, 1)), 1, 0, 6, 5, 4, 3, 2)));
        calc.Add("Veterans Day", DATE(year, 11, 11));
        calc.Add("Thanksgiving Day", DATE(year, 11, 1).AddDays(21 + CHOOSE(WEEKDAY(DATE(year, 11, 1)), 4, 3, 2, 1, 0, 6, 5)));
        calc.Add("Thanksgiving Friday", DATE(year, 11, 1).AddDays(22 + CHOOSE(WEEKDAY(DATE(year, 11, 1)), 4, 3, 2, 1, 0, 6, 5)));
        calc.Add("Christmas Eve", DATE(year, 12, 24));
        calc.Add("Christmas Day", DATE(year, 12, 25));
        calc.Add("New Years Eve", DATE(year, 12, 31));
        return calc;
    }

    public DateTime NearestWeekday(DateTime date)
    {
        return date.AddDays(date.DayOfWeek == 0 ? 1 : (date.DayOfWeek == DayOfWeek.Saturday ? -1 : 0));
    }
    '' VB Version
    Public Function vbDATE(year As Integer, month As Integer, day As Integer) As DateTime
        vbDATE = (New Date(year, month, day))
    End Function

    Public Function WEEKDAY(forDate As DateTime) As Integer
        WEEKDAY = Int(forDate.DayOfWeek) + 1
    End Function

    Public Function CHOOSE(list As Integer()) As Integer
        CHOOSE = IIf(list(0) < list.Length, list(list(0)), 0)
    End Function

    Public Function vbMOD(value As Double, modulus As Integer) As Integer
        vbMOD = value Mod modulus
    End Function

    Public Function vbINT(value As Double) As Integer
        vbINT = Int(value)
    End Function

    Public Function EasterOffset(year As Integer) As Integer
        EasterOffset = ((vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) - vbINT(vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) / 28) * (1 - vbINT(vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) / 28) * vbINT(29 / (vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) + 1)) * vbINT((21 - vbMOD(year, 19)) / 11))) - (vbMOD(year + vbINT(year / 4) + (vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) - vbINT(vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) / 28) * (1 - vbINT(vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) / 28) * vbINT(29 / (vbMOD(vbINT(year / 100) - vbINT(vbINT(year / 100) / 4) - vbINT((8 * vbINT(year / 100) + 13) / 25) + 19 * vbMOD(year, 19) + 15, 30) + 1)) * vbINT((21 - vbMOD(year, 19)) / 11))) + 2 - vbINT(year / 100) + vbINT(vbINT(year / 100) / 4), 7)) + 27)
    End Function

    Public Function CalcualteHolidays(year As Integer) As Collection
        Dim holidays As New Collection
        holidays.Add("New Years Day", vbDATE(year, 1, 1))
        holidays.Add("Martin Luther King Jr. Day", vbDATE(year, 1, 1).AddDays(14 + CHOOSE({WEEKDAY(vbDATE(year, 1, 1)), 1, 0, 6, 5, 4, 3, 2})))
        holidays.Add("Presidents Day", vbDATE(year, 2, 1).AddDays(14 + CHOOSE({WEEKDAY(vbDATE(year, 2, 1)), 1, 0, 6, 5, 4, 3, 2})))
        holidays.Add("Good Friday", vbDATE(year, 3, 1).AddDays(EasterOffset(year) - 2))
        holidays.Add("Easter Sunday", vbDATE(year, 3, 1).AddDays(EasterOffset(year)))
        holidays.Add("Memorial Day", vbDATE(year, 6, 1).AddDays(-WEEKDAY(vbDATE(year, 6, 6))))
        holidays.Add("Juneteenth", vbDATE(year, 6, 19))
        holidays.Add("Independence Day", vbDATE(year, 7, 4))
        holidays.Add("Labor Day", vbDATE(year, 9, 1).AddDays(CHOOSE({WEEKDAY(vbDATE(year, 9, 1)), 1, 0, 6, 5, 4, 3, 2})))
        holidays.Add("Columbus Day", vbDATE(year, 10, 1).AddDays(7 + CHOOSE({WEEKDAY(vbDATE(year, 10, 1)), 1, 0, 6, 5, 4, 3, 2})))
        holidays.Add("Veterans Day", vbDATE(year, 11, 11))
        holidays.Add("Thanksgiving", vbDATE(year, 11, 1).AddDays(21 + CHOOSE({WEEKDAY(vbDATE(year, 11, 1)), 4, 3, 2, 1, 0, 6, 5})))
        holidays.Add("Christmas Day", vbDATE(year, 12, 25))
        holidays.Add("New Years Eve", vbDATE(year, 12, 31))
        CalcualteHolidays = holidays
    End Function

    Public Function NearestWeekDay(holiday As DateTime) As DateTime
        NearestWeekDay = holiday.AddDays(IIf(holiday.DayOfWeek = DayOfWeek.Saturday, -1, IIf(holiday.DayOfWeek = DayOfWeek.Sunday, 1, 0)))                             
    End Function

In the logic above, I made the Current Year C2 an argument to a function that returned a list of Holiday objects. Here's the JavaScript version:

    <!DOCTYPE html><html><body>
    <h2>JavaScript Holidays</h2>
    <input id="Year" value="2022" onkeyup="GetHolidays()" />
    <p id="holidays"></p>
    <script>
    function DATE(y, m, d) { return new Date(y, m - 1, d, 0, 0, 0, 0); }
    function WEEKDAY(date) { return 1 + date.getDay(); }
    function CHOOSE(list) { return list[0]<list.length ? list[list[0]] : 0; }
    function MOD(value, modulus) { return value % modulus; }
    function INT(value) { return ~~value; }
    function AddDays(date, days) { return new Date(date.getFullYear(), date.getMonth(), date.getDate() + days); }
    class Holiday {constructor(name, date) { this.Name = name; 
                   this.Day = '   SunMonTueWedThuFriSat'.substr(WEEKDAY(date)*3,3);
                   this.Date = date; 
                   this.NearestWeekday = AddDays(date, CHOOSE([WEEKDAY(date),1,0,0,0,0,0,-1]));}}
    function GetHolidays(year) {               
      var y = year ? year : document.getElementById("Year").value;
      var holidays = [];
      holidays.push(new Holiday("New Years Day", DATE(y,1,1)));
      holidays.push(new Holiday("Martin Luther King Jr. Day", 
            AddDays(DATE(y,1,1), 14 + CHOOSE([WEEKDAY(DATE(y,1,1)),1,0,6,5,4,3,2]))));
      holidays.push(new Holiday("Presidents Day", 
            AddDays(DATE(y,2,1), 14 + CHOOSE([WEEKDAY(DATE(y,2,1)),1,0,6,5,4,3,2]))));
      var easterOffset =((MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)+
      19*MOD(y,19)+15,30)-INT(MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)
      +19*MOD(y,19)+15,30)/28)*(1-INT(MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)
      +19*MOD(y,19)+15,30)/28)*INT(29/(MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)
      +19*MOD(y,19)+15,30)+1))*INT((21-MOD(y,19))/11)))-(MOD(y+INT(y/4)+
      (MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)
      +19*MOD(y,19)+15,30)-INT(MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)
      +19*MOD(y,19)+15,30)/28)*(1-INT(MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)
      +19*MOD(y,19)+15,30)/28)*INT(29/(MOD(INT(y/100)-INT(INT(y/100)/4)-INT((8*INT(y/100)+13)/25)
      +19*MOD(y,19)+15,30)+1))*INT((21-MOD(y,19))/11)))+2-INT(y/100)+INT(INT(y/100)/4),7))+27);
      holidays.push(new Holiday("Good Friday", AddDays(DATE(y,3,1), easterOffset - 2)));
      holidays.push(new Holiday("Easter Sunday", AddDays(DATE(y,3,1), easterOffset)));
      holidays.push(new Holiday("Memorial Day", AddDays(DATE(y,6,1), -WEEKDAY(DATE(y,6,6)))));
      holidays.push(new Holiday("Juneteenth", DATE(y,6,19)));
      holidays.push(new Holiday("Independence Day", DATE(y,7,4)));
      holidays.push(new Holiday("Labor Day", 
            AddDays(DATE(y,9,1), CHOOSE([WEEKDAY(DATE(y,9,1)),1,0,6,5,4,3,2]))));
      holidays.push(new Holiday("Columbus Day", 
            AddDays(DATE(y,10,1), 7+CHOOSE([WEEKDAY(DATE(y,10,1)),1,0,6,5,4,3,2]))));
      holidays.push(new Holiday("Veterans Day", DATE(y,11,11)));
      holidays.push(new Holiday("Thanksgiving Day", 
            AddDays(DATE(y,11,1), 21+CHOOSE([WEEKDAY(DATE(y,11,1)),4,3,2,1,0,6,5]))));
      holidays.push(new Holiday("Christmas", DATE(y,12,25)));
      holidays.push(new Holiday("New Years Eve", DATE(y,12,31)));
      var html = (JSON.stringify(holidays)).split('},').join('}<br/>\r\n')
                 .split('T06:00:00.000Z').join('').split('T05:00:00.000Z').join('')
      document.getElementById("holidays").innerHTML = html;
    }
    GetHolidays(2022);
    </script>
    </body></html>

Finally, for database projects, you can define a year range and return the holidays for every year through a series of joins in SQL:

    WITH Years as (SELECT yr = year(getdate()) - 21 + delta
    FROM (SELECT RANK() OVER(ORDER BY T2.x2) AS delta
            FROM ((SELECT CAST(REPLACE(SPACE(200), ' ', '<r/>') AS xml).query('.')) AS T1(x1)
           CROSS APPLY T1.x1.nodes('r') AS T2(x2))) y), 
    Easter as (SELECT yr, offset = (((((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) 
    - CONVERT(int, (((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) / 28) 
    * (1 - CONVERT(int, (((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) / 28) 
    * CONVERT(int, 29 / ((((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) + 1))) 
    * CONVERT(int, (29 - (yr % 19)) / 11)) - ((yr + CONVERT(int, yr / 4) 
    + ((((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) 
    - CONVERT(int, (((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) / 28) 
    * (1 - CONVERT(int, (((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) / 28) 
    * CONVERT(int, 29 / ((((CONVERT(int, yr / 100)) - CONVERT(int, (CONVERT(int, yr / 100)) / 4) - CONVERT(int, (8 * (CONVERT(int, yr / 100)) + 13) / 25) + (19 * (yr % 19)) + 15) % 30) + 1))) 
    * CONVERT(int, (29 - (yr % 19)) / 11)) + 2 - (CONVERT(int, yr / 100)) + CONVERT(int, (CONVERT(int, yr / 100)) / 4)) % 7) + 27) FROM Years), 
    Dates as (SELECT HolidayName = 'New Years Day', HolidayDate = DATEFROMPARTS(yr,1,1) FROM Years
    UNION
    SELECT HolidayName = 'Martin Luther King Jr. Day',  
           HolidayDate = DATEADD(d, 14 + CHOOSE(datepart(dw, DATEFROMPARTS(yr,1,1)),1,0,6,5,4,3,2), DATEFROMPARTS(yr,1,1))
           FROM Years WHERE yr >= 1983
    UNION
    SELECT HolidayName = 'Presidents Day',  
           HolidayDate = DATEADD(d, 14 + CHOOSE(datepart(dw, DATEFROMPARTS(yr,2,1)),1,0,6,5,4,3,2), DATEFROMPARTS(yr,2,1))
           FROM Years
    UNION
    SELECT HolidayName = 'Good Friday', HolidayDate = DATEADD(d, offset - 2, DATEFROMPARTS(yr,3,1)) FROM Easter
    UNION 
    SELECT HolidayName = 'Easter', HolidayDate = DATEADD(d, offset, DATEFROMPARTS(yr,3,1)) FROM Easter
    UNION 
    SELECT HolidayName = 'Memorial Day',  
           HolidayDate = CASE WHEN yr >= 1971 THEN DATEADD(d, -datepart(dw, DATEFROMPARTS(yr,6,6)), DATEFROMPARTS(yr,6,1))
                              ELSE DATEFROMPARTS(yr,5,30) END
           FROM Years
    UNION 
    SELECT HolidayName = 'Juneteenth', HolidayDate = DATEFROMPARTS(yr,6,19) FROM Years WHERE yr >= 2021
    UNION 
    SELECT HolidayName = 'Independence Day', HolidayDate = DATEFROMPARTS(yr,7,4) FROM Years
    UNION
    SELECT HolidayName = 'Labor Day',  
           HolidayDate = DATEADD(d, CHOOSE(datepart(dw, DATEFROMPARTS(yr,9,1)),1,0,6,5,4,3,2), DATEFROMPARTS(yr,9,1))
           FROM Years WHERE yr >= 1894
    UNION
    SELECT HolidayName = 'Columbus Day',  
           HolidayDate = DATEADD(d, 7 + CHOOSE(datepart(dw, DATEFROMPARTS(yr,10,1)),1,0,6,5,4,3,2), DATEFROMPARTS(yr,10,1))
           FROM Years
    UNION 
    SELECT HolidayName = 'Veterans Day', HolidayDate = DATEFROMPARTS(yr,11,11) FROM Years WHERE yr >= 1919
    UNION
    SELECT HolidayName = 'Thanksgiving Day',  
           HolidayDate = DATEADD(d, 21 + CHOOSE(datepart(dw, DATEFROMPARTS(yr,11,1)),4,3,2,1,0,6,5), DATEFROMPARTS(yr,11,1))
           FROM Years
    UNION
    SELECT HolidayName = 'Thanksgiving Friday',  
           HolidayDate = DATEADD(d, 22 + CHOOSE(datepart(dw, DATEFROMPARTS(yr,11,1)),4,3,2,1,0,6,5), DATEFROMPARTS(yr,11,1))
           FROM Years
    UNION 
    SELECT HolidayName = 'Christmas Eve', HolidayDate = DATEFROMPARTS(yr,12,24) FROM Years
    UNION 
    SELECT HolidayName = 'Christmas Day', HolidayDate = DATEFROMPARTS(yr,12,25) FROM Years
    UNION 
    SELECT HolidayName = 'New Years Eve', HolidayDate = DATEFROMPARTS(yr,12,31) FROM Years)

    SELECT [Sort] = RANK() OVER(ORDER BY HolidayDate),
           HolidayName, [Day] = SUBSTRING('  SunMonTueWedThuFriSat', datepart(dw, HolidayDate)*3, 3),
           HolidayDate, NearestWeekday = DATEADD(d, CHOOSE(datepart(dw, HolidayDate),1,0,0,0,0,0,-1), HolidayDate)
    FROM Dates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.