[AccessD] DatePart "WW"

Gustav Brock Gustav at cactus.dk
Thu May 24 09:44:23 CDT 2007


Hi Mark

If you are looking for ISO week numbers, use these constants:

  bytWeek = DatePart("ww", datDate, vbMonday, vbFirstFourDays)

However, DatePart has always - since Access 1.0 - been buggy for week number 53.
Thus, for serious business use, use a function like this which I have posted many times:

Public Function ISO_WeekNumber( _
  ByVal datDate As Date) _
  As Byte

' Calculates and returns week number for date datDate according to the ISO 8601:1988 standard.
' 1998-2000, Gustav Brock, Cactus Data ApS, CPH.
' May be freely used and distributed.

  Const cbytFirstWeekOfAnyYear  As Byte = 1
  Const cbytLastWeekOfLeapYear  As Byte = 53
  
  Dim bytWeek                   As Byte
  Dim bytISOThursday            As Byte
  Dim datLastDayOfYear          As Date

  bytWeek = DatePart("ww", datDate, vbMonday, vbFirstFourDays)
  
  If bytWeek = cbytLastWeekOfLeapYear Then
    bytISOThursday = WeekDay(vbThursday, vbMonday)
    datLastDayOfYear = DateSerial(Year(datDate), 12, 31)
    If WeekDay(datLastDayOfYear, vbMonday) >= bytISOThursday Then
      ' OK, week count of 53 is caused by leap year.
    Else
      ' Correct for Access97/2000 bug.
      bytWeek = cbytFirstWeekOfAnyYear
    End If
  End If
  
  ISO_WeekNumber = bytWeek

End Function

/gustav

>>> markamatte at hotmail.com 24-05-2007 16:14 >>>
Hello All,

I'm using datepart to get the week.  12/31/2006 I am having an issue...No 
matter what optional constant I use or change...I get Day1 of week 53... and 
1/1/2007 shows as Day2 of week 1.  How do I get Day1 of Week1?

Thanks,

Mark A. Matte






More information about the AccessD mailing list