Calculate weekends of the week in strings

I am currently writing a small calendar in ASP.Net C #. Currently, to create the weeks strings, I am doing the following for the loop:

var iWeeks = 6; for (int w = 0; w < iWeeks; w++) { 

This works great, but within a month you will only have 5 weeks, and in some rare cases, 4.

How can I calculate the number of rows that will be needed for a particular month?

This is an example of what I am creating:

enter image description here

As you can see from the above month, only 5 rows are required. Take this month (August 2008), which started on Saturday and ends on Monday of the 6th week / row.

Image found on google


This is an example of what I am creating:

enter image description here

As you can see from the above month, only 5 rows are required. Take this month (August 2008), which started on Saturday and ends on Monday of the 6th week / row.

Image found on google

+7
source share
9 answers

Here is the way:

 public int GetWeekRows(int year, int month) { DateTime firstDayOfMonth = new DateTime(year, month, 1); DateTime lastDayOfMonth = new DateTime(year, month, 1).AddMonths(1).AddDays(-1); System.Globalization.Calendar calendar = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; int lastWeek = calendar.GetWeekOfYear(lastDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); int firstWeek = calendar.GetWeekOfYear(firstDayOfMonth, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); return lastWeek - firstWeek + 1; } 

You can customize the calendar week rule by changing the System.Globalization.CalendarWeekRule.FirstFourDayWeek part. I hope the code itself clarifies.

+6
source share

Well, it depends on the culture you use, but let it be assumed that you can use Thread.CurrentThread.CurrentCulture, then the code to get the week today is:

 Culture culture = Thread.CurrentThread.CurrentCulture; Calendar cal = culture.Calendar; Int32 week = cal.GetWeekOfYear(DateTime.Today, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek); 
+2
source share

How to check what week the first and last days will be?

0
source share

The months in the Julian / Gregorian calendar have the same number of days each year, except February, which can have 28 or 29 days depending on the jump of the year. You can find the number of days in the Description section at http://en.wikipedia.org/wiki/Gregorian_calendar .

As @darkdog pointed out, you have DateTime.DaysInMonth. Just do the following:

 var days = DateTime.DaysInMonth(year, month) + WhatDayOfWeekTheMonthStarts(year, month); int rows = (days / 7); if (0 < days % 7) { ++rows; } 

Take into account the fact that for the purposes of globalization / localization, different calendars / methods of organizing the year are used in some parts of the world.

0
source share

Try it,

 DateTime.DaysInMonth 
0
source share

Check Calendar.GetWeekOfYear . He has to do the trick.

There is a problem with this, it does not comply with the 4 days rule according to ISO 8601, but otherwise it is neat.

0
source share

You can get the days of the month using DateTime.DaysInMonth (int WhatYear, int WhichMonth);

0
source share

The problem is not the number of days in a month, but how many weeks it covers.

February in a non-leap year will have 28 days, and if the first day of the month is Monday, February will be exactly 4 weeks.

However, if the first day of the month is Tuesday or any other day of the week, February will cover 5-week numbers.

A 31-day month can span 5 or 6 weeks in the same way. If the month starts on Monday, 31 days gives you 5-week numbers. If the month starts on Saturday or Sunday, it will cover 6 weeks.

Thus, the correct way to get this number is to find the week number on the first and last days of the month.


Change # 1 : here, how to calculate the number of weeks in a given month: Change # 2 : fixed bugs in the code

 public static Int32 GetWeekForDateCurrentCulture(DateTime dt) { CultureInfo culture = Thread.CurrentThread.CurrentCulture; Calendar cal = culture.Calendar; return cal.GetWeekOfYear(dt, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek); } public static Int32 GetWeekSpanCountForMonth(DateTime dt) { DateTime firstDayInMonth = new DateTime(dt.Year, dt.Month, 1); DateTime lastDayInMonth = firstDayInMonth.AddMonths(1).AddDays(-1); return GetWeekForDateCurrentCulture(lastDayInMonth) - GetWeekForDateCurrentCulture(firstDayInMonth) + 1; } 
0
source share

First find out which day of the week is the first day of the month. Just a new date and time from the first day, always 1 and the year and month on which there is a week week property.

Then from here you can use the number of days in the month DateTime.DaysInMonth to determine how many weeks you divide by seven, and then add the number of days from 1 that your first day falls on. For example,

 public static int RowsForMonth(int month, int year) { DateTime first = new DateTime(year, month, 1); //number of days pushed beyond monday this one sits int offset = ((int)first.DayOfWeek) - 1; int actualdays = DateTime.DaysInMonth(month, year) + offset; decimal rows = (actualdays / 7); if ((rows - ((int)rows)) > .1) { rows++; } return rows; } 
0
source share

All Articles