Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get nth weekday of month in C# [duplicate]

Tags:

date

c#

dayofweek

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

like image 955
Prasad Avatar asked Jul 19 '10 19:07

Prasad


People also ask

How to calculate Nth day of month?

To calculate the Nth weekday, we need a formula to do 3 things: Find the starting date of the selected month and year. Find week with the Nth occurrence (4th, in this example) Add or subtract days, to get to selected weekday (day 5, in this example)

How do you calculate weekdays in a month?

Our main formula is the NETWORKDAYS function, which counts the workdays between the start date and end date, excluding weekends and holidays. The start date is the 1st day of the month, B3, while the end date is given by the EOMONTH function. EOMONTH returns the last day of a month.

How can I get 3rd Friday in a month?

To get the last Friday of the month, find the first Friday of the next month and subtract 7 days. To get the 3rd Friday of the month, add 14 days to the first Friday.

How do you find the weekday from a date?

For a Gregorian date, add 0 for 1900's, 6 for 2000's, 4 for 1700's, 2 for 1800's; for other years, add or subtract multiples of 400. For a Julian date, add 1 for 1700's, and 1 for every additional century you go back. Add the last two digits of the year. Divide by 7 and take the remainder.


2 Answers

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)

like image 184
SLaks Avatar answered Oct 03 '22 04:10

SLaks


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.
like image 42
Ian Jacobs Avatar answered Oct 03 '22 06:10

Ian Jacobs