12

Possible Duplicate:
How do I determine if a given date is Nth weekday of the month?

How do i get the nth weekday of the month?

For ex.:

2nd Monday of "July 2010" = 07/12/2010.

Looking for a function like:

public DateTime GetNthWeekofMonth(DateTime date, int nthWeek, DayOfWeek dayofWeek)
{
//return the date of nth week of month
}

from the above, the parameters of the function will be ("Any Date in July 2010", 2, Monday).

Community
  • 1
  • 1
Prasad
  • 58,881
  • 64
  • 151
  • 199
  • It's not clear what input and output you expect here. Please clarify. – Winston Smith Jul 19 '10 at 19:49
  • I added an answer to a similar question that returns a list of DateTime, in case you need more than just a single DateTime at http://stackoverflow.com/questions/288513/how-do-i-determine-if-a-given-date-is-the-nth-weekday-of-the-month/35584324#35584324 – B. Clay Shannon-B. Crow Raven Feb 23 '16 at 17:35

4 Answers4

25

Use the following extension method:

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); 
    }
}

You can then write

new DateTime(2010, 07, 01).Next(DayOfWeek.Monday).AddDays((2 - 1) * 7);

Or, as a function:

public DateTime GetNthWeekofMonth(DateTime date, int nthWeek, DayOfWeek dayOfWeek) {
    return date.Next(dayOfWeek).AddDays((nthWeek - 1) * 7);
}

(I need to subtract one because date.Next(dayOfWeek) is already the first occurrence of that day)

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    The OP didn't specify what should happen if the n-th day is no longer within the desired month, but I feel the input should somehow be validated. – Dirk Vollmar Jul 19 '10 at 20:19
  • @SLaks, thanks for the answer. @0xA3, i have also validated the input such that, if the week number is >4, i'll get the last week of the month. – Prasad Jul 19 '10 at 20:28
  • Brilliant Code -- thank you! Very Very Minor point... To use in code... change "dayofWeek" in the last function "GetNthWeekofMonth" to dayOfWeek... (capitalizing the "O" to match the line that follows). – Danimal111 Jun 04 '14 at 00:21
  • If I do `GetNthWeekofMonth(new DateTime(2010, 7, 30), 2, DayOfWeek.Monday)`, I get 8/9/2010 as the result.. wouldn't 2nd week of Monday in July 30, 2010 be 7/12/2010? – stack247 Jun 12 '15 at 00:28
  • 1
    I change @SLaks implementation of `GetNthWeekOfMonth` to the following, should work: `return new DateTime(date.Year, date.Month, 1).Next(dayofWeek).AddDays((nthWeek - 1) * 7);` – stack247 Jun 12 '15 at 00:42
  • To build on @stack247's comment, you can clamp the results to the last occurrence by making it recursive: var next = Next(new DateTime(date.Year, date.Month, 1), dayOfWeek).AddDays((nthWeek - 1) * 7); if (date.Month != next.Month && nthWeek > 1) next = date.GetNthWeekOfMonth(nthWeek - 1, dayOfWeek); return next; – Rob10e Jan 04 '18 at 19:05
3

One possible algorithm:

  1. Start from the 1st of the month.
  2. Move forward one day at a time until you get the Day of Week you're looking for.
  3. Add (7 * N) to the date you're on to get the date you want.
Ian Jacobs
  • 5,456
  • 1
  • 23
  • 38
  • 1
    Instead of step (2), you could just find what day of the week is the first day and add the requisite number of days to that day. For example, if the 1st is a Tuesday you know the 1st Monday would be the 7th (+6). In general if you define your days with Monday = 1, Tuesday = 2, ... , Sunday = 7 A formula for the 1st X day of the month would be the piecewise function: X-Y+1 if X>Y 8-Y+X if X – Assaf Jul 19 '10 at 19:59
0

Duplicate can be found here: How do I determine if a given date is the Nth weekday of the month?

int d = date.Day; 
return date.DayOfWeek == dow && (d-1)/7 == (n-1); 
Community
  • 1
  • 1
JonH
  • 32,732
  • 12
  • 87
  • 145
0
IEnumerable<DateTime> WeekdaysFrom( DateTime start )
{
    DateTime weekday = start.Add( TimeSpan.FromDays(1) );
    while( weekday < DateTime.MaxValue.Subtract( TimeSpan.FromDays(1) ) )
    {
        while( weekday.DayOfWeek == DayOfWeek.Saturday || weekday.DayOfWeek == DayOfWeek.Sunday )
        {
            weekday.Add( TimeSpan.FromDays(1) );
        }
        yield return weekday;
    }
}

DateTime NthWeekday( DateTime month, int n )
{
    return WeekdaysFrom( new DateTime( month.year, month.month, 1 ) ).Skip(n-1).First();
}
maxwellb
  • 13,366
  • 2
  • 25
  • 35