2012-11-13 17 views
1

我想列出未來10周我如何列出未來幾周?

結果應該是這樣的:

Week  Year 
---------------- 
45:   2012 
46:   2012 
47:   2012 
48:   2012 
49:   2012 
50:   2012 
51:   2012 
52:   2012 
1:   2013 
2:   2013 

幾年有一個53周,而今年12月31日是星期一,是本週1或53周?

無論如何,我想跳過第53周,每當它發生。這意味着1或2天不會成爲任何一週的一部分,但這並不重要。

+0

您遇到了什麼問題,這部分用?你有嘗試過什麼嗎? – Max

回答

1

我只是寫了一個小控制檯應用程序,做到了這一點:今天

static void Main(string[] args) 
    { 
     System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; 
     DateTime dt = DateTime.Now; //Use current date as starting point 
     for (int i = 0; i < 10; i++) 
     { 

      int weekNo = ci.Calendar.GetWeekOfYear(
      dt, 
      ci.DateTimeFormat.CalendarWeekRule, 
      ci.DateTimeFormat.FirstDayOfWeek 
      ); 
      int year = ci.Calendar.GetYear(dt); 
      if (weekNo == 53) //if week number==53, then go to next week 
      { 
       dt = dt.AddDays(7); 
       weekNo = ci.Calendar.GetWeekOfYear(
       dt, 
       ci.DateTimeFormat.CalendarWeekRule, 
       ci.DateTimeFormat.FirstDayOfWeek 
       ); 
       year = ci.Calendar.GetYear(dt); 
      } 

      dt = dt.AddDays(7); 
      Console.WriteLine(weekNo + "-" + year); 
     } 
    } 

輸出:

46-2012 
    47-2012 
    48-2012 
    49-2012 
    50-2012 
    51-2012 
    52-2012 
    1-2013 
    2-2013 
    3-2013 
0

從MSDN http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

using System; 
using System.Globalization; 

public class Example 
{ 
    public static void Main() 
    { 
     DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo; 
     DateTime date1 = new DateTime(2011, 1, 1); 
     Calendar cal = dfi.Calendar; 

     Console.WriteLine("{0:d}: Week {1} ({2})", date1, 
         cal.GetWeekOfYear(date1, dfi.CalendarWeekRule, 
              dfi.FirstDayOfWeek), 
         cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1));  
    } 
} 
// The example displays the following output: //  1/1/2011: Week 1 (GregorianCalendar) 
2

幾年有一個53周,而今年12月31日是一個星期一,這是第1周或53周?

假設你的意思是「一週的星期」,這將是本週年第1周2013年

在ISO日曆,星期,一年的第一週是第一個週一週日有4天或更長時間。

目前尚不清楚爲什麼你想跳過第53周 - 它不只是跳過1或2天,它會跳過整整一週。

當然,這實際上假設你的意思是「年份周」的ISO定義。如果你不這樣做,這是另一回事。在做其他事情之前,你需要澄清你的要求。

要從.NET獲取「星期幾」,您可以使用Calendar.GetWeekOfYear - 對於ISO定義,您將使用CalendarWeekRule.FirstFourDayWeekDayOfWeek.Monday。儘管如此,我不知道是否有什麼可以獲得本週的一年。

由於公然插頭,在我Noda Time有會議日期都WeekYearWeekOfWeekYear支持,您可以構建一個給定的一週年/周的週年/星期幾的組合的日期。

+0

我以爲第53周至多會有2天的時間,在這種情況下,我不需要它在名單上......但沒關係,不管長度如何,最好還是在那裏。 –

-1

以下將基於自年初以來的星期數進行工作。

如果您希望基於完整的星期,則需要根據一年的第一週的某一天確定起始位置的偏移量0或1。

public class Week 
{ 
    public Week(int weekOfYear, int year) 
    { 
     WeekOfYear = weekOfYear; 
     Year = year; 
    } 
    public int WeekOfYear { get; private set; } 
    public int Year { get; private set; } 
} 
public IEnumerable<Week> Next10Weeks(DateTime startDate) 
{ 
    DateTime tempDate = startDate; 
    for (int i = 0; i < 10; i++) 
    { 
     //add one to first parameter if you want the 1-indexed week instead of 0 indexed 
     yield return new Week(tempDate.DayOfYear % 7, tempDate.Year); 
     tempDate.AddDays(7); 
    } 
}