2010-01-06 144 views
0

我試圖修復一個函數,該函數返回給定年份中的週數。按日期計算週數

下面是它的外觀:

Function GetWeekNo(date) 
    weekOfYear = DatePart("ww", DateValue(date), vbMonday, vbFirstFourDays) 

    If weekOfYear > 52 Then 
     If DatePart("ww", DateValue(date) + 7, vbMonday, vbFirstFourDays) = 2 Then 
      weekOfYear = 1 
     End If 
    End If 

    GetWeekNo = weekOfYear 
End Function 

當此功能給出的日期2010年12月31日返回52共有53周,2010年

注:我沒有經典的ASP體驗,無論如何。

回答

4

似乎取決於哪一週被認爲是「一年中的第一週」。

DatePart("ww", "12/31/2010", vbMonday) 
' returns 53 
' FirstWeekOfYear parameter defaults to vbFirstJan1 
' the week that contains January/01/2010 
' here, its the week starting on December/28/2009 

DatePart("ww", "12/31/2010", vbMonday, vbFirstFourDays) 
' returns 52 
' FirstWeekOfYear parameter set to vbFirstFourDays 
' the first week that has at least four days of the new year 
' here, its the week starting on January/04/2010 

DatePart("ww", "12/31/2010", vbMonday, vbFirstFullWeek) 
' returns 52 
' FirstWeekOfYear parameter set to vbFirstFullWeek 
' the first week that has full seven days of the new year 
' here, again, its the week starting on January/04/2010 

決定什麼是您對一年的第一週的定義,然後相應地使用DatePart函數。

+0

有道理。這是否適用於所有文化? – roosteronacid 2010-01-06 10:32:09

+0

不,不一定。我建議你堅持使用默認的vbFirstJan1,這對大多數人來說都是有意義的,儘管也有例外,例如,很多人總是希望週數達到52。 – 2010-01-06 10:36:28