2010-01-25 62 views
33

.NET庫是否可以簡單地返回給定日期的星期數?例如,輸入Year = 2010, Month = 1, Day = 25,應輸出5爲週數。使用.NET計算一個月中的第幾周

最近我發現是Calendar.GetWeekOfYear,這幾乎就在那裏。

Java有一個日期字符串格式「W」,它在一個月內返回一週,但我在.NET中看不到任何等同的東西。

回答

49

沒有內置的方式來做到這一點,但這裏是一個擴展方法,它應該爲你做的工作:

static class DateTimeExtensions { 
    static GregorianCalendar _gc = new GregorianCalendar(); 
    public static int GetWeekOfMonth(this DateTime time) { 
     DateTime first = new DateTime(time.Year, time.Month, 1); 
     return time.GetWeekOfYear() - first.GetWeekOfYear() + 1; 
    } 

    static int GetWeekOfYear(this DateTime time) { 
     return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); 
    } 
} 

用法:

DateTime time = new DateTime(2010, 1, 25); 
Console.WriteLine(time.GetWeekOfMonth()); 

輸出:

5 

您可以根據自己的需要更改GetWeekOfYear

4

,那麼具體:http://forums.asp.net/t/1268112.aspx

你可以使用:

public static int Iso8601WeekNumber(DateTime dt) 
{ 
    return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); 
} 

public static int GetWeekOfMonth(DateTime dt) 
{  

    int weekOfYear = Iso8601WeekNumber(dt); 

    if (dt.Month == 1) 
    { 
     //week of year == week of month in January 
     //this also fixes the overflowing last December week 
     return weekOfYear; 
    } 

    int weekOfYearAtFirst = Iso8601WeekNumber(dt.AddDays(1 - dt.Day)); 
    return weekOfYear - weekOfYearAtFirst + 1; 
} 

注意認爲,由於有一定的算法選擇,您的需求可能會有所不同。即是一週的星期一到星期五是二月底還是三月的第一週,或者什麼標準定義了一個月的「第一個」星期? ISO 8601:2004未給出「每週」的定義。

+0

這是不正確的;它會爲'dt = new DateTime(2010,1,25)'返回'-48'。 – jason 2010-01-26 00:21:07

+0

@Jason很好。問題在於一月的第一天屬於十二月的最後一週。修復了忽略該代碼的代碼。 – GaussZ 2010-01-26 07:53:09

18

沒有直接的內置方式來做到這一點,但它可以很容易地完成。這是一個擴展方法,它可以用來輕鬆獲得日期的年份爲基礎的週數:

public static int GetWeekNumber(this DateTime date) 
{ 
    return GetWeekNumber(date, CultureInfo.CurrentCulture); 
} 

public static int GetWeekNumber(this DateTime date, CultureInfo culture) 
{ 
    return culture.Calendar.GetWeekOfYear(date, 
     culture.DateTimeFormat.CalendarWeekRule, 
     culture.DateTimeFormat.FirstDayOfWeek); 
} 

然後,我們可以用它來計算月爲週數,有點像Jason shows。一個文化友好的版本可能看起來像這樣:

public static int GetWeekNumberOfMonth(this DateTime date) 
{ 
    return GetWeekNumberOfMonth(date, CultureInfo.CurrentCulture); 
} 

public static int GetWeekNumberOfMonth(this DateTime date, CultureInfo culture) 
{ 
    return date.GetWeekNumber(culture) 
     - new DateTime(date.Year, date.Month, 1).GetWeekNumber(culture) 
     + 1; // Or skip +1 if you want the first week to be 0. 
} 
+0

+ 1對文化友善 – eddiegroves 2010-01-26 21:14:12

+0

@eddiegroves:謝謝。至少有人讚賞它:p – Svish 2010-01-26 21:49:26

0

這是一個古老的線程,但我需要這樣的事情,這就是我想出的。

public static int WeekOfMonth(this DateTime date) 
{ 
    DateTime firstDayOfMonth = new DateTime(date.Year, date.Month, 1); 
    int firstDay = (int)firstDayOfMonth.DayOfWeek; 
    if (firstDay == 0) 
    { 
     firstDay = 7; 
    } 
    double d = (firstDay + date.Day - 1)/7.0; 
    return (int)Math.Ceiling(d); 
} 

注意:如果您想使用星期一作爲一週的第一天,這應該有效。如果你想讓這個星期在星期天開始,那麼在2015年3月的第二個星期以及其他日期可能會失敗。 2015年3月9日星期一是第二週的第一天。上述計算在該日期返回「3」。

0
public int WeekCalc(DateTime dt) 
{ 
    CultureInfo ciCurr = CultureInfo.CurrentCulture; 
    int weekNum = ciCurr.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); 
    return weekNum; 
} 
2

@Svish

VB版:

Public Shared Function GetWeekNumber(ByVal [date] As DateTime) As Integer 
    Return GetWeekNumber([date], CultureInfo.CurrentCulture) 
End Function 

Public Shared Function GetWeekNumber(ByVal [date] As DateTime, ByVal culture As CultureInfo) As Integer 
    Return culture.Calendar.GetWeekOfYear([date], culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek) 
End Function 

Public Shared Function GetWeekNumberOfMonth(ByVal [date] As DateTime) As Integer 
    Return GetWeekNumberOfMonth([date], CultureInfo.CurrentCulture) 
End Function 

Public Shared Function GetWeekNumberOfMonth(ByVal [date] As DateTime, ByVal culture As CultureInfo) As Integer 
    Return GetWeekNumber([date], culture) - GetWeekNumber(New DateTime([date].Year, [date].Month, 1), culture) + 1 
    ' Or skip +1 if you want the first week to be 0. 
End Function 
0

一旦你有一個月的第一天的星期幾,你可以使用整數運算得到一個月的一週。

static class DateTimeExtensions 
{ 
    // Assumes that in current culture week starts on Sunday 
    public static int GetWeekOfMonth(this DateTimeOffset time) 
    { 
     DateTimeOffset first = new DateTimeOffset(time.Year, time.Month, 1, 0, 0, 0, time.Offset); 
     int firstSunday = (7 - (int)first.DayOfWeek) % 7 + 1; 
     int weekOfMonth = 1 + (time.Day + 7 - firstSunday)/7; 

     return weekOfMonth; 
    } 
} 
0

一些簡單的...:)

private int WeeksNumber() 
     { 
      int daysInMonth = DateTime.DaysInMonth(YourYear, YourMonth); 
      DateTime date = new DateTime(Year, Month, 1); 
      switch (daysInMonth) 
      { 
       case 28: 
        { 
         if (date.DayOfWeek == DayOfWeek.Monday) 
         { 
          return 4; 
         } 
         else 
         { 
          return 5; 
         } 
        } 
       case 29: 
        { 
         return 5; 
        } 
       case 30: 
        { 
         if (date.DayOfWeek == DayOfWeek.Sunday) 
         { 
          return 6; 
         } 
         else 
         { 
          return 5; 
         } 
        } 
       case 31: 
        { 
         if (date.DayOfWeek == DayOfWeek.Sunday || date.DayOfWeek == DayOfWeek.Saturday) 
         { 
          return 6; 
         } 
         else 
         { 
          return 5; 
         } 
        } 
       default: return 5; 
      } 
     } 
11

標題是誤導。問題實際上是關於月份的日曆周編號(如果您計算日曆中的行的週數),而不是實際的週數。

月內的週數,你可以使用:

VB: 
    Function WeekNumber(TargetDate As Date) As Integer 
     Return (TargetDate.Day - 1) \ 7 + 1 
    End Function 

C#: 
    public int WeekNumber(DateTime TargetDate) 
    { 
     return (TargetDate.Day - 1)/7 + 1; 
    } 
+1

我同意這個標題是誤導性的。例如,我試圖計算本月的第四個星期一。但是,當我使用此問題提供的其他答案編寫單元測試時,其中一些結果與我預期的不符。這是因爲如果一個月的第一天是星期二或以後的一週,結果會關閉一週。這個答案提供的代碼是我最終用來解決問題的。 – user3308241 2015-01-23 05:35:17

+1

這不符合這個例子。 2010年1月1日是星期五,所以第一個日曆周只有兩天。因此,第25周通常會在第4周使用這個公式計算,實際上是在第5周。由於該例說2010年1月1日應該產生第5周,所以這個答案中的公式不可能是正確的,因爲它產生第4周(即(int((25-1)/ 7)+1)= 4. – Triynko 2016-02-18 04:31:49

2

這是我的解決方案:

static string GetWeekNumber() 
    { 

     var weekNumber = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, 
      DayOfWeek.Sunday) - 
     CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now.AddDays(1 - DateTime.Now.Day), 
      CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday) + 1; 

     switch (weekNumber) 
     { 
      case 1 : 
       return "First"; 
      case 2: 
       return "Second"; 
      case 3: 
       return "Third"; 
      default: 
       return "Fourth"; 
     } 
    } 
+0

你知道每個月可能有6周嗎? – 2016-06-09 21:09:16

1

這裏有一個簡單的選擇,如果你想第7天被認爲是第一週,即使一個月不上週日開始:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var dates = Enumerable.Range(0, 10) 
      .Select(r => new DateTime(2015, 10, 1).AddDays(r)) 
      .Select(q => new { Date = q, WeekOfMonth = q.WeekOfMonth() }); 

     foreach(var item in dates) 
     { 
      Console.WriteLine("Date: {0:ddd MM-dd-yyyy}, WOM: {1}", item.Date, item.WeekOfMonth); 
     } 

     var final = Console.ReadLine(); 
    } 
} 

public static class DateTimeExtensions 
{ 
    public static int WeekOfMonth(this DateTime d) 
    { 
     var isExact = (d.Day % 7 == 0); 
     var offset = isExact ? 0 : 1; 
     return (int)Math.Floor(d.Day/7.0) + offset; 
    } 
} 
0
Dim mdt As Date = txtDT.Text 
    Dim weekNum As Integer = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(mdt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday) - CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(GetFirstDayOfMonth(mdt), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday) + 1 
    Response.Write(weekNum)