<p>I am currently writing a small calendar in ASP.Net C#. Currently to produce the rows of the weeks I do the following for loop:</p>

<pre><code>var iWeeks = 6;
for (int w = 0; w &lt; iWeeks; w++) {
</code></pre>

<p>This works fine, however, some month will only have 5 weeks and in some rare cases, 4.</p>

<p>How can I calculate the number of rows that will be required for a particular month?</p>

<p>This is an example of what I am creating:</p>

<p><img src="https://i.stack.imgur.com/NkxN7.png" alt="enter image description here"></p>

<p>As you can see for the above month, there are only 5 rows required, however. Take the this month (August 2008) which started on a Saturday and ends on a Monday on the 6th Week/Row.</p>

<p><strong><em>Image found on google</em></strong></p>

<hr>

<p>This is an example of what I am creating:</p>

<p><img src="https://i.stack.imgur.com/NkxN7.png" alt="enter image description here"></p>

<p>As you can see for the above month, there are only 5 rows required, however. Take the this month (August 2008) which started on a Saturday and ends on a Monday on the 6th Week/Row.</p>

<p><strong><em>Image found on google</em></strong></p>

[{'answer_id': 9806, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 0, 'selected': False, 'text': '<p>How about checking which week the first and last days will be in?</p>\n'}, {'answer_id': 9810, 'author': 'kokos', 'author_id': 1065, 'author_profile': 'https://Stackoverflow.com/users/1065', 'pm_score': 0, 'selected': False, 'text': '<p>The months in the Julian / Gregorian calendar have the same number of days each year, except February who can have 28 or 29 days depending on the leapness of the year. You can find the number of days in the Description section at <a href="http://en.wikipedia.org/wiki/Gregorian_calendar" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Gregorian_calendar</a>.</p>\n\n<p>As @darkdog mentioned you have DateTime.DaysInMonth. Just do this:</p>\n\n<pre><code>var days = DateTime.DaysInMonth(year, month) + \n           WhatDayOfWeekTheMonthStarts(year, month);    \nint rows = (days / 7);  \nif (0 &lt; days % 7)  \n{  \n    ++rows;\n}  \n</code></pre>\n\n<p>Take into consideration the fact that for globalization / localization purposes, some parts of the world use different calendars / methods of organization of the year.</p>\n'}, {'answer_id': 9815, 'author': 'darkdog', 'author_id': 1094, 'author_profile': 'https://Stackoverflow.com/users/1094', 'pm_score': 0, 'selected': False, 'text': '<p>Try this,</p>\n\n<pre><code>DateTime.DaysInMonth\n</code></pre>\n'}, {'answer_id': 9821, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 2, 'selected': False, 'text': "<p>Well, it depends on the culture you're using, but let's assume you can use Thread.CurrentThread.CurrentCulture, then the code to get the week of today would be:</p>\n\n<pre><code>Culture culture = Thread.CurrentThread.CurrentCulture;\nCalendar cal = culture.Calendar;\nInt32 week = cal.GetWeekOfYear(DateTime.Today,\n    culture.DateTimeFormat.CalendarWeekRule,\n    culture.DateTimeFormat.FirstDayOfWeek);\n</code></pre>\n"}, {'answer_id': 9822, 'author': 'Biri', 'author_id': 968, 'author_profile': 'https://Stackoverflow.com/users/968', 'pm_score': 0, 'selected': False, 'text': '<p>Check <a href="https://learn.microsoft.com/en-in/dotnet/api/system.globalization.calendar.getweekofyear" rel="nofollow noreferrer">Calendar.GetWeekOfYear</a>. It should do the trick.</p>\n<p>There is a problem with it, it does not follow the 4 day rule by ISO 8601, but otherwise it is neat.</p>\n'}, {'answer_id': 9827, 'author': 'darkdog', 'author_id': 1094, 'author_profile': 'https://Stackoverflow.com/users/1094', 'pm_score': 0, 'selected': False, 'text': '<p>you can get the days of a month by using DateTime.DaysInMonth(int WhichYear,int WhichMonth);</p>\n'}, {'answer_id': 9830, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 0, 'selected': False, 'text': "<p>The problem isn't the number of days in the month, it's how many weeks it spans over.</p>\n\n<p>February in a non-leap year will have 28 days, and if the first day of the month is a monday, february will span exactly 4 week numbers.</p>\n\n<p>However, if the first day of the month is a tuesday, or any other day of the week, february will span 5 week numbers.</p>\n\n<p>A 31 day month can span 5 or 6 weeks the same way. If the month starts on a monday, the 31 days gives you 5 week numbers. If the month starts on saturday or sunday, it will span 6 week numbers.</p>\n\n<p>So the right way to obtain this number is to find the week number of the first and last days of the month.</p>\n\n<hr>\n\n<p><strong>Edit #1</strong>: Here's how to calculate the number of weeks a given month spans:\n<strong>Edit #2</strong>: Fixed bugs in code</p>\n\n<pre><code>public static Int32 GetWeekForDateCurrentCulture(DateTime dt)\n{\n    CultureInfo culture = Thread.CurrentThread.CurrentCulture;\n    Calendar cal = culture.Calendar;\n    return cal.GetWeekOfYear(dt,\n        culture.DateTimeFormat.CalendarWeekRule,\n        culture.DateTimeFormat.FirstDayOfWeek);\n}\n\npublic static Int32 GetWeekSpanCountForMonth(DateTime dt)\n{\n    DateTime firstDayInMonth = new DateTime(dt.Year, dt.Month, 1);\n    DateTime lastDayInMonth = firstDayInMonth.AddMonths(1).AddDays(-1);\n    return\n        GetWeekForDateCurrentCulture(lastDayInMonth)\n        - GetWeekForDateCurrentCulture(firstDayInMonth)\n        + 1;\n}\n</code></pre>\n"}, {'answer_id': 9844, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 0, 'selected': False, 'text': '<p>First Find out which weekday the first day of the month is in. Just new up a datetime with the first day, always 1, and the year and month in question, there is a day of week property on it.</p>\n\n<p>Then from here, you can use the number of days in the month, <code>DateTime.DaysInMonth</code>, in order to determine how many weeks when you divide by seven and then add the number of days from 1 that your first day falls on. For instance,</p>\n\n<pre><code>public static int RowsForMonth(int month, int year)\n{\n    DateTime first = new DateTime(year, month, 1);\n\n    //number of days pushed beyond monday this one sits\n    int offset = ((int)first.DayOfWeek) - 1;\n\n    int actualdays = DateTime.DaysInMonth(month, year) +  offset;\n\n    decimal rows = (actualdays / 7);\n    if ((rows - ((int)rows)) &gt; .1)\n    {\n        rows++;\n    }\n    return rows;\n}\n</code></pre>\n'}, {'answer_id': 9851, 'author': 'Serhat Ozgel', 'author_id': 31505, 'author_profile': 'https://Stackoverflow.com/users/31505', 'pm_score': 4, 'selected': True, 'text': '<p>Here is the method that does it:</p>\n\n<pre><code>public int GetWeekRows(int year, int month)\n{\n    DateTime firstDayOfMonth = new DateTime(year, month, 1);\n    DateTime lastDayOfMonth = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);\n    System.Globalization.Calendar calendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;\n    int lastWeek = calendar.GetWeekOfYear(lastDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);\n    int firstWeek = calendar.GetWeekOfYear(firstDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);\n    return lastWeek - firstWeek + 1;\n}\n</code></pre>\n\n<p>You can customize the calendar week rule by modifying the System.Globalization.CalendarWeekRule.FirstFourDayWeek part. I hope the code is self explanatory.</p>\n'}]
['https://Stackoverflow.com/questions/9805', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/383/']