开发者

Calculate the number of weeks by date

开发者 https://www.devze.com 2022-12-15 09:22 出处:网络
I am trying to fix a function which returns the number of weeks in a given year. Here\'s how it looks:

I am trying to fix a function which returns the number of weeks in a given year.

Here's how it looks:

Function GetWe开发者_运维问答ekNo(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

When this function is given the date 12-31-2010 it returns 52. There are 53 weeks in 2010.

Note: I have no experience with classic ASP, what-so-ever.


Seems like it depends on which week is considered as the "first week of the year".

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

Decide what is your definition of the first week of the year, then use the DatePart function accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消