如何计算/查找给定日期的周数?
如何计算/查找给定日期的周数?
var currentCulture = CultureInfo.CurrentCulture; var weekNo = currentCulture.Calendar.GetWeekOfYear( new DateTime(2013, 12, 31), currentCulture.DateTimeFormat.CalendarWeekRule, currentCulture.DateTimeFormat.FirstDayOfWeek);
请注意,这不 符合ISO 8601标准 。 在瑞典,我们使用ISO 8601 周数,但即使文化设置为“sv-SE”, CalendarWeekRule
为FirstFourDayWeek
, FirstDayOfWeek
为周一, 周变量将设置为53而不是上述代码中的正确1 。
我只用瑞典设置试过这个,但我很确定使用ISO 8601周数的所有国家(奥地利,德国,瑞士等)都会受到这个问题的影响。
Peter van Ooijen和Shawn Steele对这个问题有不同的解决方案。
这是一个紧凑的解决方案
private static int WeekOfYearISO8601(DateTime date) { var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date); return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); }
它已经过以下测试
var datesAndISO8601Weeks = new Dictionary { {new DateTime(2000, 12, 31), 52}, {new DateTime(2001, 1, 1), 1}, {new DateTime(2005, 1, 1), 53}, {new DateTime(2007, 12, 31), 1}, {new DateTime(2008, 12, 29), 1}, {new DateTime(2010, 1, 3), 53}, {new DateTime(2011, 12, 31), 52}, {new DateTime(2012, 1, 1), 52}, {new DateTime(2013, 1, 2), 1}, {new DateTime(2013, 12, 31), 1}, }; foreach (var dateWeek in datesAndISO8601Weeks) { Debug.Assert(WeekOfYearISO8601(dateWeek.Key) == dateWeek.Value, dateWeek.Key.ToShortDateString() + " should be week number " + dateWeek.Value + " but was " + WeekOfYearISO8601(dateWeek.Key)); }
public static int GetWeekNumber(DateTime dtPassed) { CultureInfo ciCurr = CultureInfo.CurrentCulture; int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); return weekNum; }
在MSDN上查看GetWeekOfYear有这个例子:
using System; using System.Globalization; public class SamplesCalendar { public static void Main() { // Gets the Calendar instance associated with a CultureInfo. CultureInfo myCI = new CultureInfo("en-US"); Calendar myCal = myCI.Calendar; // Gets the DTFI properties required by GetWeekOfYear. CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule; DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek; // Displays the number of the current week relative to the beginning of the year. Console.WriteLine( "The CalendarWeekRule used for the en-US culture is {0}.", myCWR ); Console.WriteLine( "The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW ); Console.WriteLine( "Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear( DateTime.Now, myCWR, myFirstDOW )); // Displays the total number of weeks in the current year. DateTime LastDay = new System.DateTime( DateTime.Now.Year, 12, 31 ); Console.WriteLine( "There are {0} weeks in the current year ({1}).", myCal.GetWeekOfYear( LastDay, myCWR, myFirstDOW ), LastDay.Year ); } }
My.Computer.Info.InstalledUICulture.DateTimeFormat.Calendar.GetWeekOfYear(yourDateHere, CalendarWeekRule.FirstDay, My.Computer.Info.InstalledUICulture.DateTimeFormat.FirstDayOfWeek)
这样的东西……
我知道这已经很晚了,但是因为它出现在我的搜索中,我想我会抛出一个不同的解决方案。这是ac#solution。
(int)(Math.Ceiling((decimal)startDate.Day / 7)) + (((new DateTime(startDate.Year, startDate.Month, 1).DayOfWeek) > startDate.DayOfWeek) ? 1 : 0);
如果你想要ISO 8601周数,其中几周从星期一开始,所有星期都是七天,而第一周是包含一年中第一个星期四的一周,这可能是一个解决方案。
由于似乎没有.Net文化可以产生正确的ISO-8601周数,我会全部绕过内置周确定,并手动进行计算,而不是试图纠正部分正确的结果。
我最终得到的是以下扩展方法:
public static int GetIso8601WeekNumber(this DateTime date) { var thursday = date.AddDays(3 - ((int)date.DayOfWeek + 6) % 7); return 1 + (thursday.DayOfYear - 1) / 7; }
首先,((int)date.DayOfWeek + 6)%7)确定工作日数,0 =星期一,6 =星期日。
date.AddDays( – ((int)date.DayOfWeek + 6)%7)确定所请求周数之前的星期一的日期。
三天后是星期四的目标,它决定了一周的哪一年。
如果您将年内(零基础)日期数除以7(向下舍入),您将获得该年度的(零基础)周数。
在c#中,整数计算结果是隐式舍入的。
var cultureInfo = CultureInfo.CurrentCulture; var calendar = cultureInfo.Calendar;
var calendarWeekRule = cultureInfo.DateTimeFormat.CalendarWeekRule; var firstDayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek; var lastDayOfWeek = cultureInfo.LCID == 1033 //En-us ? DayOfWeek.Saturday : DayOfWeek.Sunday; var lastDayOfYear = new DateTime(date.Year, 12, 31); //Check if this is the last week in the year and it doesn`t occupy the whole week var weekNumber = calendar.GetWeekOfYear(date, calendarWeekRule, firstDayOfWeek); return weekNumber == 53 && lastDayOfYear.DayOfWeek != lastDayOfWeek ? 1 : weekNumber;
它适用于美国和俄罗斯文化。 ISO 8601也是正确的,因为俄罗斯周从星期一开始。