I was needing to check for Holidays in an Appointment calendar and found some decent code that needed a bit of refactoring.
http://geekswithblogs.net/wpeck/archive/2011/12/27/us-holiday-list-in-c.aspx
static HashSet<DateTime> GetHolidays(int year) { var holidays = new HashSet<DateTime>{ NewYears(year), MemorialDay(year), IndependenceDay(year), LaborDay(year), Thanksgiving(year), Christmas(year) }; var next = NextNewYears(year); if(next != null) holidays.Add(next.Value); return holidays; } static DateTime NewYears(int year){ return AdjustForWeekendHoliday(new DateTime(year, 1, 1)); } static DateTime IndependenceDay(int year){ return AdjustForWeekendHoliday(new DateTime(year, 7, 4)); } /// <summary> /// Memorial Day is the last Monday in May /// </summary> /// <param name="year"></param> /// <returns></returns> static DateTime MemorialDay(int year){ var day = new DateTime(year, 5, 31); while (day.DayOfWeek != DayOfWeek.Monday) { day = day.AddDays(-1); } return day; } /// <summary> /// Labor Day is the first Monday in September /// </summary> /// <param name="year"></param> /// <returns></returns> static DateTime LaborDay(int year) { var day = new DateTime(year, 9, 1); while (day.DayOfWeek != DayOfWeek.Monday) { day = day.AddDays(1); } return day; } /// <summary> /// Thanksgiving Day is the fourth Thursday in November /// </summary> /// <param name="year"></param> /// <returns></returns> static DateTime Thanksgiving(int year) { var thanksgiving = (from date in Enumerable.Range(1, 30) where new DateTime(year, 11, date).DayOfWeek == DayOfWeek.Thursday select date).ElementAt(3); var day = new DateTime(year, 11, thanksgiving); return day; } static DateTime Christmas(int year){ return AdjustForWeekendHoliday(new DateTime(year, 12, 25)); } /// <summary> /// If next year's new years is adjusted for this year, then count it /// </summary> /// <param name="year"></param> /// <returns></returns> static DateTime? NextNewYears(int year){ var day = AdjustForWeekendHoliday(new DateTime(year + 1, 1, 1)); if (day.Year == year) return day; return null; } /// <summary> /// If the holiday falls on a Saturday, then make it Friday. /// If the holiday falls on a Sunday, then make it Monday. /// </summary> /// <param name="holiday"></param> /// <returns></returns> public static DateTime AdjustForWeekendHoliday(DateTime holiday) { if (holiday.DayOfWeek == DayOfWeek.Saturday) { return holiday.AddDays(-1); } if (holiday.DayOfWeek == DayOfWeek.Sunday) { return holiday.AddDays(1); } return holiday; }