2010-01-06 313 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