Ok, had to quickly write this one, to find the nth day of a month.
private System.DateTime SetDayOfMonth(System.Int32 nth, System.DayOfWeek weekday, System.Int32 month, System.Int32 year)
{
System.DateTime dath = new DateTime(year, month, 1); //set to first day of the month
//get to the first weekday looking for
while(dath.DayOfWeek != weekday)
{
dath = dath.AddDays(1);
}
if(nth > 1)
{
dath = dath.AddDays((nth - 1) * 7);
}
else
{
dath = dath.AddMonths(1);
dath = dath.Substract(TimeSpan.FromDays(1); //last day of the month
while(dath.DayOfWeek != weekday)
{
dath = dath.Subtract(TimeSpan.FromDays(1)); //cycle backwards to last
}
dath = dath.Subtract(TimeSpan.FromDays(((nth * -1) - 1) * 7));
}
return dath;
}
No comments:
Post a Comment