Geeks With Blogs
Wil Peck Experience is something you don't get until just 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.

private static HashSet<DateTime> GetHolidays(int year)
            HashSet<DateTime> holidays = new HashSet<DateTime>();
            //NEW YEARS
            DateTime newYearsDate = AdjustForWeekendHoliday(new DateTime(year, 1, 1).Date);
            //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;
            //INDEPENCENCE DAY
            DateTime independenceDay = AdjustForWeekendHoliday(new DateTime(year, 7, 4).Date);
            //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;

            //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);

            DateTime christmasDay = AdjustForWeekendHoliday(new DateTime(year, 12, 25).Date);
            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);
                return holiday;

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

Comments on this post: US Holiday List in C#

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Wil Peck | Powered by: | Join free