If what you are looking for is the nth particular weekday of a month in a year, this code may help:
using System;
class NthWeekDayOfMonth
{
public
NthWeekDayOfMonth(DateTime date)
{
this.date = date;
}
public
NthWeekDayOfMonth(int n, DayOfWeek weekDay, int month, int year)
{
DateTime firstDayOfMonth = new DateTime(year, month, 1);
if ( weekDay < firstDayOfMonth.DayOfWeek )
{
this.date = firstDayOfMonth.AddDays((n - 1) * 7 + weekDay + 7 - firstDayOfMonth.DayOfWeek);
}
else
{
this.date = firstDayOfMonth.AddDays((n - 1) * 7 + weekDay - firstDayOfMonth.DayOfWeek);
}
}
public int
Month
{
get { return date.Month; }
}
public DayOfWeek
WeekDay
{
get { return date.DayOfWeek; }
}
public int
N
{
get { return (date.Day - 1) / 7 + 1; }
}
public int
Year
{
get { return date.Year; }
}
public DateTime
Date
{
get { return date; }
}
private DateTime
date;
}
class Program
{
static void Main(string[] args)
{
for ( DateTime d = new DateTime(2010, 7, 1); d <= new DateTime(2010, 7, 31); d = d.AddDays(1) )
{
NthWeekDayOfMonth thisYear = new NthWeekDayOfMonth(d);
NthWeekDayOfMonth lastYear = new NthWeekDayOfMonth(thisYear.N, thisYear.WeekDay, thisYear.Month, thisYear.Year - 1);
Console.WriteLine("{0}th {1} of {2} in {3}: {4} - in {5}: {6}", thisYear.N, thisYear.WeekDay, thisYear.Month, thisYear.Year, thisYear.Date, lastYear.Year, lastYear.Date);
}
}
}