如何计算/查找给定日期的周数?

时间:2009-09-30 11:34:26

标签: c# asp.net date

如何计算/查找给定日期的周数?

7 个答案:

答案 0 :(得分:72)

var currentCulture = CultureInfo.CurrentCulture;
var weekNo = currentCulture.Calendar.GetWeekOfYear(
                new DateTime(2013, 12, 31),
                currentCulture.DateTimeFormat.CalendarWeekRule,
                currentCulture.DateTimeFormat.FirstDayOfWeek);

请注意, ISO 8601兼容。在瑞典,我们使用ISO 8601 week数字,但即使文化设置为“sv-SE”,CalendarWeekRuleFirstFourDayWeekFirstDayOfWeek为星期一 weekNo 变量将设置为 53 ,而不是上述代码中的正确 1

我只尝试过使用瑞典语设置,但我很确定使用ISO 8601周数的所有国家/地区(奥地利,德国,瑞士等)都会受到此问题的影响。

Peter van OoijenShawn 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<DateTime, int>
                        {
                            {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));
}

答案 1 :(得分:4)

  public static int GetWeekNumber(DateTime dtPassed)
  {
          CultureInfo ciCurr = CultureInfo.CurrentCulture;
          int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
          return weekNum;
  }

答案 2 :(得分:4)

在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 );

   }

}

答案 3 :(得分:1)

My.Computer.Info.InstalledUICulture.DateTimeFormat.Calendar.GetWeekOfYear(yourDateHere, CalendarWeekRule.FirstDay, My.Computer.Info.InstalledUICulture.DateTimeFormat.FirstDayOfWeek)

像这样......

答案 4 :(得分:1)

我知道这已经很晚了,但是因为它出现在我的搜索中,我想我会抛出一个不同的解决方案。这是一个c#解决方案。

(int)(Math.Ceiling((decimal)startDate.Day / 7)) + (((new DateTime(startDate.Year, startDate.Month, 1).DayOfWeek) > startDate.DayOfWeek) ? 1 : 0);  

答案 5 :(得分:1)

如果你想要ISO 8601周数,其中几周从星期一开始,所有星期都是七天,而第一周是包含一年中第一个星期四的一周,这可能是一个解决方案。

由于似乎没有.Net文化可以产生正确的ISO-8601周数,我会全部绕过内置周确定,并手动进行计算,而不是试图纠正部分正确的结果。

我最终得到的是以下扩展方法:

C

首先,((int)date.DayOfWeek + 6)%7)确定工作日编号,0 =星期一,6 =星期日。

date.AddDays( - ((int)date.DayOfWeek + 6)%7)确定所请求周数之前的星期一的日期。

三天后是星期四的目标,它决定了一周中的哪一年。

如果您将年内的(从零开始)日期数除以7(向下舍入),您将获得该年度的(零基础)周数。

在c#中,整数计算结果是隐式舍入的。

答案 6 :(得分:0)

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也是正确的,因为俄罗斯周从星期一开始。