views:

264

answers:

1

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.

+3  A: 

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.

Salman A
Makes sense. Does this apply for all cultures?
roosteronacid
No, not necessarily. I suggest you stick with the default, vbFirstJan1, it makes sense to most of the people although there are exceptions, for example, many people will always expect the number of weeks to be 52.
Salman A