Geeks With Blogs
Wil Peck Experience is something you get right after you need it.

We automate monitoring of our applications 24/7/365.  However, there are certain days of the year we don’t want to get notified depending on the type of monitoring we are doing.  Especially when we are monitoring for stale dates on various processes when our customers in the US aren’t doing business.  Below are a couple of simple functions that get a list of US holidays that you can use in your applications.  I used a couple references.

http://en.wikipedia.org/wiki/Public_holidays_in_the_United_States

http://www.archives.gov/federal-register/codification/executive-order/11582.html

private static HashSet<DateTime> GetHolidays(int year)
        {
            HashSet<DateTime> holidays = new HashSet<DateTime>();
            //NEW YEARS
            DateTime newYearsDate = AdjustForWeekendHoliday(new DateTime(year, 1, 1).Date);
            holidays.Add(newYearsDate);
            //MEMORIAL DAY  -- last monday in May
            DateTime memorialDay = new DateTime(year, 5, 31);
            DayOfWeek dayOfWeek = memorialDay.DayOfWeek;
            while (dayOfWeek != DayOfWeek.Monday)
            {
                memorialDay = memorialDay.AddDays(-1);
                dayOfWeek = memorialDay.DayOfWeek;
            }
            holidays.Add(memorialDay.Date);
           
            //INDEPENCENCE DAY
            DateTime independenceDay = AdjustForWeekendHoliday(new DateTime(year, 7, 4).Date);
            holidays.Add(independenceDay);
        
            //LABOR DAY -- 1st Monday in September
            DateTime laborDay = new DateTime(year, 9, 1);
            dayOfWeek = laborDay.DayOfWeek;
            while(dayOfWeek != DayOfWeek.Monday)
            {
                laborDay = laborDay.AddDays(1);
                dayOfWeek = laborDay.DayOfWeek;
            }
            holidays.Add(laborDay.Date);

            //THANKSGIVING DAY - 4th Thursday in November
            var thanksgiving = (from day in Enumerable.Range(1, 30)
                           where new DateTime(year, 11, day).DayOfWeek == DayOfWeek.Thursday
                           select day).ElementAt(3);
            DateTime thanksgivingDay = new DateTime(year, 11, thanksgiving);
            holidays.Add(thanksgivingDay.Date);

            DateTime christmasDay = AdjustForWeekendHoliday(new DateTime(year, 12, 25).Date);
            holidays.Add(christmasDay);
            return holidays;
        }

        public static DateTime AdjustForWeekendHoliday(DateTime holiday)
        {
            if (holiday.DayOfWeek == DayOfWeek.Saturday)
            {
                return holiday.AddDays(-1);
            }
            else if (holiday.DayOfWeek == DayOfWeek.Sunday)
            {
                return holiday.AddDays(1);
            }
            else
            {
                return holiday;
            }
        }

Posted on Tuesday, December 27, 2011 6:20 AM | Back to top


Comments on this post: US Holiday List in C#

# re: US Holiday List in C#
Requesting Gravatar...
Great article. Exactly what I needed.

I also put together a way to call GetHolidays and exit the app if the current date is a holiday.

string dtToday = DateTime.Now.ToShortDateString();

List<DateTime> GH = GetHolidays(dtCurrentYear).ToList<DateTime>()

foreach (DateTime gh in GH)
{
if(gh.ToShortDateString() == dtToday)
{
System.Environment.Exit(0);
}

}
Left by Clark Bohs on Dec 25, 2014 9:02 AM

# re: US Holiday List in C# -- MLK + President's Days
Requesting Gravatar...
Great article! Very useful. Below are blocks for Martin Luther King day and President's Day.

//Martin Luther King Day -- third monday in January
DateTime MLKDay = new DateTime(year, 1, 21);
DayOfWeek dayOfWeek = MLKDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
{
MLKDay = MLKDay.AddDays(-1);
dayOfWeek = MLKDay.DayOfWeek;
}
holidays.Add(MLKDay.Date);


//President's Day -- third monday in February
DateTime PresDay = new DateTime(year, 2, 21);
dayOfWeek = PresDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
{
PresDay = PresDay.AddDays(-1);
dayOfWeek = PresDay.DayOfWeek;
}
holidays.Add(PresDay.Date);

Left by Clark Bohs on Jan 19, 2015 8:25 PM

# re: US Holiday List in C#
Requesting Gravatar...
Thanks Clark! I'll have to get those added.
Left by Wil on Jan 20, 2015 9:29 AM

# re: US Holiday List in C#
Requesting Gravatar...
this is good link for good friday:

http://stackoverflow.com/questions/2510383/how-can-i-calculate-what-date-good-friday-falls-on-given-a-year

DateTime GoodFriday = EasterSunday(year).AddDays(-2);
holidays.Add(GoodFriday);
Left by Mike on Apr 02, 2015 10:32 AM

Your comment:
 (will show your gravatar)


Copyright © Wil Peck | Powered by: GeeksWithBlogs.net | Join free