将DateTime周计算为行
我目前正在ASP.Net C#中编写一个小日历。当前要生成星期几的行,请执行以下for循环:
var iWeeks = 6; for (int w = 0; w < iWeeks; w++) {
这可以正常工作,但是某些月份只有5周,在极少数情况下为4周。
如何计算特定月份所需的行数?
这是我正在创建的示例:
如我们所见,上个月只需要5行。以本月(2008年8月)开始于本月(星期六),结束于第六周/行的星期一。
在Google上找到的图片
这是我正在创建的示例:
正如我们在上个月所看到的,仅需要5行。以本月(2008年8月)为例,该月从星期六开始,在第六周/行的星期一结束。
在Google上找到的图片
解决方案
回答
如何检查第一天和最后几天在哪一周?
回答
朱利安/格里高利历中的月份每年具有相同的天数,但2月除外,根据年份的跨度,二月可以有28天或者29天。我们可以在http://en.wikipedia.org/wiki/Gregorian_calendar的"说明"部分中找到天数。
正如@darkdog提到的,我们具有DateTime.DaysInMonth。只需执行以下操作:
var days = DateTime.DaysInMonth(year, month) + WhatDayOfWeekTheMonthStarts(year, month); int rows = (days / 7); if (0 < days % 7) { ++rows; }
考虑到以下事实:出于全球化/本地化的目的,世界上某些地区使用不同的年度日历/组织方法。
回答
试试这个,
DateTime.DaysInMonth
回答
好吧,这取决于我们使用的区域性,但是假设我们可以使用Thread.CurrentThread.CurrentCulture,那么获取今天星期的代码将是:
Culture culture = Thread.CurrentThread.CurrentCulture; Calendar cal = culture.Calendar; Int32 week = cal.GetWeekOfYear(DateTime.Today, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek);
回答
检查Calendar.GetWeekOfYear。它应该可以解决问题。
它有一个问题,它没有遵循ISO 8601的4天规则,但它整洁。
回答
我们可以使用DateTime.DaysInMonth(int WhoYear,int whichMonth)获得一个月的日期;
回答
问题不在于一个月中的天数,而是它跨越多少周。
在非le年的2月将有28天,如果每月的第一天是星期一,则2月将恰好跨越4周。
但是,如果每月的第一天是星期二或者一周中的任何其他天,则2月将跨越5周。
一个31天的月份可以用相同的方式跨越5或者6个星期。如果该月从星期一开始,则31天为我们提供5周的数字。如果月份从星期六或者星期日开始,则将跨6个星期。
因此,获取此数字的正确方法是找到该月第一天和最后几天的星期几。
编辑#1:以下是计算给定月份跨度的周数的方法:
编辑#2:修复了代码中的错误
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; }
回答
首先找出一个月的第一天是哪个工作日。只需将日期时间与第一天(通常为1)和所涉及的年月月份进行更新,就可以确定星期几。
然后从这里开始,我们可以使用月份中的天数" DateTime.DaysInMonth"来确定除以7的周数,然后从第一天开始算起从1开始的天数。例如,
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; }
回答
这是执行此操作的方法:
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; }
我们可以通过修改System.Globalization.CalendarWeekRule.FirstFourDayWeek部件来自定义日历周规则。我希望代码能自我解释。