次の拡張メソッドを使用します。
public static class DateTimeExtensions
{
///<summary>Gets the first week day following a date.</summary>
///<param name="date">The date.</param>
///<param name="dayOfWeek">The day of week to return.</param>
///<returns>The first dayOfWeek day following date, or date if it is on dayOfWeek.</returns>
public static DateTime Next(this DateTime date, DayOfWeek dayOfWeek) {
return date.AddDays((dayOfWeek < date.DayOfWeek ? 7 : 0) + dayOfWeek - date.DayOfWeek);
}
}
その後、書くことができます
new DateTime(2010, 07, 01).Next(DayOfWeek.Monday).AddDays((2 - 1) * 7);
または、関数として:
public DateTime GetNthWeekofMonth(DateTime date, int nthWeek, DayOfWeek dayOfWeek) {
return date.Next(dayOfWeek).AddDays((nthWeek - 1) * 7);
}
date.Next(dayOfWeek)
(すでにその日の最初の出現であるため、1を引く必要があります)