Calculating holidays

Posted by Ralph Shillington on Stack Overflow See other posts from Stack Overflow or by Ralph Shillington
Published on 2010-12-22T15:46:38Z Indexed on 2010/12/22 15:55 UTC
Read the original article Hit count: 242

Filed under:
|
|

A number of holidays move around from year to year. For example, in Canada Victoria day (aka the May two-four weekend) is the Monday before May 25th, or Thanksgiving is the 2nd Monday of October (in Canada).

I've been using variations on this Linq query to get the date of a holiday for a given year:

var year = 2011;
var month = 10;
var dow = DayOfWeek.Monday;
var instance = 2;

var day = (from d in Enumerable.Range(1,DateTime.DaysInMonth(year,month))
let sample = new DateTime(year,month,d)
where sample.DayOfWeek == dow
select sample).Skip(instance-1).Take(1);

While this works, and is easy enough to understand, I can imagine there is a more elegant way of making this calculation versus this brute force approach.

Of course this doesn't touch on holidays such as Easter and the many other lunar based dates.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about algorithm