0

I am trying to find the number of weekday of month by passing a date in C#.

For ex.: If I pass "3/6/2013 (6-Mar-2013)" the function should say 1st wednesday of month. If I pass "3/1/2013 (1-Mar-2013)" the function should say 1st friday of month. If I pass "3/13/2013 (13-Mar-2013)" the function should say 2nd wednesday of month.

I was trying to use this function, but its returning the week of month(i.e. passing "3/6/2013 (6-Mar-2013)" returns 2):

public static int GetWeekOfMonth(DateTime date)
        {
            DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);

            while (date.Date.AddDays(1).DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
                date = date.AddDays(1);

            return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;
        }

Please suggest on how to get the number of weekday of month.

Prasad
  • 58,881
  • 64
  • 151
  • 199

3 Answers3

1

Here's something I threw together real quick:

public static int GetWeekOfMonth(DateTime dateTime)
{
    DayOfWeek dayOfWeek = dateTime.DayOfWeek;
    DateTime dayStep = new DateTime(dateTime.Year, dateTime.Month, 1);
    int returnValue = 0;

    while (dayStep <= dateTime)
    {
        if (dayStep.DayOfWeek == dayOfWeek)
        {
            returnValue++;
        }

        dayStep = dayStep.AddDays(1);
    }

    return returnValue;
}

Here is the same method, only it returns a string such as "1st", "2nd", "3rd", etc, instead of a simple INT. The ordinal code is based on samjudson's code on this post: Is there an easy way to create ordinals in C#?

public static string GetWeekOfMonth(DateTime dateTime)
{
    DayOfWeek dayOfWeek = dateTime.DayOfWeek;
    DateTime dayStep = new DateTime(dateTime.Year, dateTime.Month, 1);
    int returnValue = 0;

    while (dayStep <= dateTime)
    {
        if (dayStep.DayOfWeek == dayOfWeek)
        {
            returnValue++;
        }

        dayStep = dayStep.AddDays(1);
    }

    switch (returnValue % 100)
    {
        case 11:
        case 12:
        case 13:
            return returnValue.ToString() + "th";
    }

    switch (returnValue % 10)
    {
        case 1:
            return returnValue.ToString() + "st";
        case 2:
            return returnValue.ToString() + "nd";
        case 3:
            return returnValue.ToString() + "rd";
        default:
            return returnValue.ToString() + "th";
    }
}

And finally, this will return "2nd Wednesday of month":

DateTime dt = DateTime.Parse("2013-03-13");
string dateString = string.Format("{0} {1} of month", GetWeekOfMonth(dt), dt.ToString("dddd"));
Community
  • 1
  • 1
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
1
public static int GetWeekOfMonth(DateTime date)
{
  var weekDay = date.DayOfWeek;
  var nth = date.Day / 7;
  if (date.Day % 7 > 0)
  {
    nth++;
  }
  return nth;
}

Edit: var weekDay... is actually not needed.

Ramunas
  • 3,853
  • 1
  • 30
  • 31
1

It's really simple, I think. If the day of month is 1 through 7, the result is 1, if the day of month is 8 through 14, it's 2, and so on. Therefore:

public static int GetWeekOfMonth(DateTime date)
{
    return (date.Day + 6) / 7;
}

This works because of the way division / is defined when the operands (dividend and divisor) are integers, see / Operator (C# Reference).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181