Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get the number of weeks for a month? Thereby I want to get all weeks, even the one which are not full weeks for this month.
Posted

A 30 day month has 4 2/7 weeks' worth of days, but those days may not start or end on a whole week. This you already know it being the nature of your problem.

If the first day of the month lines up with the beginning of a week - you know the month covers 5 weeks - four whole weeks and the 'spare days' starting the fifth week. For a 30 day month, provided it starts anywhere between the first and the sixth day of the week this will be true.

If the first day of the month lines up with the SEVENTH day of the week, however, you're into the 'six week' mode.

For a 31 day month, that last case includes the sixth and seventh day of the week.

For a 28 or 29 day month - I'm sure you can figure out the pattern yourself.

So - I would create four lookup tables (one each for 28,29,30,31 days in a month), each with seven entries. The entry contains the number of days in a month; you index into the table based on the weekday of the first on the month.
 
Share this answer
 
Comments
bfb 16-Mar-15 11:40am    
Thanks. Do you have a suggestion of how to implement the lookup table?
MSDN Style:
C#
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;

var currentWeekno = cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);

So create a first day of month date and a last day of month date and you have the weeknumbers.
 
Share this answer
 
v2
Comments
bfb 16-Mar-15 9:16am    
There is a week number problematic, when the first day of a year isn't a Monday. So the question is when is the beginning of a week counted?

Also for the current month (2015-03-16) it outputs 4. It should be 6!
bfb 16-Mar-15 9:54am    
I know that there are 4.4 weeks, but I need the span of the number of weeks of a month. Therefore I have to calculate all incomplete weeks at the beginning and end. In the worst case this are 6 weeks.
Richard Deeming 16-Mar-15 10:47am    
digimanus's code will give you what you're looking for:

int startWeek = cal.GetWeekOfYear(new DateTime(2015, 3, 1), dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
int endWeek = cal.GetWeekOfYear(new DateTime(2015, 3, 31), dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
int numberOfWeeks = 1 + endWeek - startWeek; // == 6
bfb 16-Mar-15 10:54am    
I tried it only in a dotnetfiddle: https://dotnetfiddle.net/pWJFgO

Here the output is 5. I don't know why. And why are you adding one week?
Richard Deeming 16-Mar-15 11:10am    
Looks like that's down to a difference in the first day of week settings. My computer has Monday, whereas .NET Fiddle has Sunday. If you replace dfi.FirstDayOfWeek with DayOfWeek.Monday, you'll get six weeks.

You might also need to replace dfi.CalendarWeekRule with CalendarWeekRule.FirstDay to avoid months starting in week 53 of the previous year.

And I'm adding 1 because you want to include the start week.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900