Get nth Week day of a Month

Following is the code snippet for getting the nth week day of any Month.
public DateTime GetNthWeekDayOfMonth(int nthWeek, int month, int year, DayOfWeek dayOfWeek)
{
    DateTime date = new DateTime(year, month, 1);
    // Find the first occurence of the weekday 
    date = date.AddDays((dayOfWeek < date.DayOfWeek ? 7 : 0) + dayOfWeek - date.DayOfWeek);
    // Find nth week day by adding the n-1 weeks 
    return date.AddDays((nthWeek - 1) * 7);
}

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment