views:

243

answers:

4

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).

+2  A: 

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
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>Y8-Y+X if X<Y1 if X=Y Where Y is the weekday of the 1st of the month.
Assaf
A: 

Duplicate can be found here: http://stackoverflow.com/questions/288513/how-do-i-determine-if-a-given-date-is-nth-weekday-of-the-month

int d = date.Day; 
return date.DayOfWeek == dow && (d-1)/7 == (n-1); 
JonH
downvote for ??
JonH
No vote from me, but this is not an answer to the (updated) question.
0xA3
+3  A: 

Use the following extension method:

///<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)

SLaks
Why was this downvoted?
SLaks
+1 to offset; net 8
Jay
@Jay: Actually, net 0; I hit the reputation limit long ago.
SLaks
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.
0xA3
@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
@0xA3: That's up to him.
SLaks
A: 
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
A loop is completely unnecessary here.
SLaks