Today I had a need to check if the current date was a holiday. I wrote up this quick class to do the calculations. This may be a good class to add as an extension to the DateTime class as well. It is one of those nice simple classes that is always useful.
public class Holiday
{
public static bool IsHoliday(DateTime date)
{
return
Holiday.IsNewYearsDay(date)
|| Holiday.IsNewYearsEve(date)
|| Holiday.IsThanksgivingDay(date)
|| Holiday.IsDayAfterThanksgiving(date)
|| Holiday.IsChristmasEve(date)
|| Holiday.IsChristmasDay(date)
|| Holiday.IsFourthOfJuly(date)
|| Holiday.IsLaborDay(date)
|| Holiday.IsMemorialDay(date);
}
public static bool IsNewYearsDay(DateTime date)
{
return date.DayOfYear == AdjustForWeekendHoliday(new DateTime(date.Year, 1, 1)).DayOfYear;
}
public static bool IsNewYearsEve(DateTime date)
{
return date.DayOfYear == AdjustForWeekendHoliday(new DateTime(date.Year, 12, 31)).DayOfYear;
}
public static bool IsChristmasEve(DateTime date)
{
return date.DayOfYear == AdjustForWeekendHoliday(new DateTime(date.Year, 12, 24)).DayOfYear;
}
public static bool IsChristmasDay(DateTime date)
{
return date.DayOfYear == AdjustForWeekendHoliday(new DateTime(date.Year, 12, 25)).DayOfYear;
}
public static bool IsFourthOfJuly(DateTime date)
{
return date.DayOfYear == AdjustForWeekendHoliday(new DateTime(date.Year, 7, 4)).DayOfYear;
}
public static bool IsLaborDay(DateTime date)
{ // First Monday in September
DateTime laborDay = new DateTime(date.Year, 9, 1);
DayOfWeek dayOfWeek = laborDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
{
laborDay = laborDay.AddDays(1);
dayOfWeek = laborDay.DayOfWeek;
}
return date.DayOfYear == laborDay.DayOfYear;
}
public static bool IsMemorialDay(DateTime date)
{ //Last Monday in May
DateTime memorialDay = new DateTime(date.Year, 5, 31);
DayOfWeek dayOfWeek = memorialDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
{
memorialDay = memorialDay.AddDays(-1);
dayOfWeek = memorialDay.DayOfWeek;
}
return date.DayOfYear == memorialDay.DayOfYear;
}
public static bool IsThanksgivingDay(DateTime date)
{//4th Thursday in November
var thanksgiving = (from day in Enumerable.Range(1, 30)
where new DateTime(date.Year, 11, day).DayOfWeek == DayOfWeek.Thursday
select day).ElementAt(3);
DateTime thanksgivingDay = new DateTime(date.Year, 11, thanksgiving);
return date.DayOfYear == thanksgivingDay.DayOfYear;
}
public static bool IsDayAfterThanksgiving(DateTime date)
{//Day after Thanksgiving
var thanksgiving = (from day in Enumerable.Range(1, 30)
where new DateTime(date.Year, 11, day).DayOfWeek == DayOfWeek.Thursday
select day).ElementAt(3);
DateTime thanksgivingDay = new DateTime(date.Year, 11, thanksgiving + 1);
return date.DayOfYear == thanksgivingDay.DayOfYear;
}
private 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;
}
}
}
For thanksgiving it would be 3x faster to get the first thursday and add 21.
var thanksgiving = (from day in Enumerable.Range(1, 7)
where new DateTime(year, 11, day).DayOfWeek == DayOfWeek.Thursday
select day).First();
DateTime thanksgivingDay = new DateTime(year, 11, thanksgiving + 21);
return thanksgivingDay;
LikeLike