cllpse
cllpse

Reputation: 21727

Calculate the number of weeks by date

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

Here's how it looks:

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

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.

Upvotes: 0

Views: 3841

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272046

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.

Upvotes: 4

Related Questions