按日期计算周数

时间:2010-01-06 09:37:02

标签: date vbscript asp-classic

我正在尝试修复一个函数,该函数返回给定年份的周数。

以下是它的外观:

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

当给出此功能时,日期为12-31-2010,它返回52. 2010年有53周。

注意:我没有使用经典ASP的经验,无论如何。

1 个答案:

答案 0 :(得分: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函数。