I want to calculate the number of weeks within a month.

The first week of January 2014 starting from the first Monday is the 6th. So, January has 4 weeks. The first week of March 2014 starting from the first Monday is the 3rd. So, March has 5 weeks.

I want to know how many weeks there are in a month counting from the first Monday, not the first day.

How do I do this?

I have this code but it is used to get week number of the month for specific dates.

public int GetWeekNumberOfMonth(DateTime date)
{
    date = date.Date;
    DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
    DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    if (firstMonthMonday > date)
    {
        firstMonthDay = firstMonthDay.AddMonths(-1);
        firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    }
    return (date - firstMonthMonday).Days / 7 + 1;
}

3 Answers

15
Cyral On Best Solutions

Try this:

Get the number of days in the current month, and find the first day. For each day in the month, see if the day is a Monday, if so, increment the value.

public static int MondaysInMonth(DateTime thisMonth)
{
    int mondays = 0;
    int month = thisMonth.Month;
    int year = thisMonth.Year;
    int daysThisMonth = DateTime.DaysInMonth(year, month);
    DateTime beginingOfThisMonth = new DateTime(year, month, 1);
    for (int i = 0; i < daysThisMonth; i++)
        if (beginingOfThisMonth.AddDays(i).DayOfWeek == DayOfWeek.Monday)
            mondays++;
    return mondays;
}

You can use it like this with the current date:

Console.WriteLine(MondaysInMonth(DateTime.Now));

Output: 4

or with any month you choose:

Console.WriteLine(MondaysInMonth(new DateTime(year, month, 1)))
2
Sivakrishna Donepudi On

Just correction to Cyral code :

i must start from 0 as he is using AddDays method .

Reason :the above example returns 5 for NOV and 4 for DEC which is wrong..

Edited code :

    public static int MondaysInMonth(DateTime thisMonth)
{
    int mondays = 0;
    int month = thisMonth.Month;
    int year = thisMonth.Year;
    int daysThisMonth = DateTime.DaysInMonth(year, month);
    DateTime beginingOfThisMonth = new DateTime(year, month, 1);
    for (int i = 0; i < daysThisMonth; i++)
        if (beginingOfThisMonth.AddDays(i).DayOfWeek == DayOfWeek.Monday)
            mondays++;
    return mondays;
}
1
Tiago Ávila On

I tried to use that code, but in some cases it didn't work. So I found this code on MSDN.

public static int MondaysInMonth(this DateTime time)
    {
        //extract the month
        int daysInMonth = DateTime.DaysInMonth(time.Year, time.Month);
        var firstOfMonth = new DateTime(time.Year, time.Month, 1);
        //days of week starts by default as Sunday = 0
        var firstDayOfMonth = (int)firstOfMonth.DayOfWeek;
        var weeksInMonth = (int)Math.Ceiling((firstDayOfMonth + daysInMonth) / 7.0);

        return weeksInMonth;
    }

I created it like a extension method, so I can use like that:

var dateTimeNow = DateTime.Now;
var weeks = dateTimeNow.MondaysInMonth();