[dba-VB] DatePart "WW" (C#: Extension of DateTime)

Gustav Brock gustav at cactus.dk
Tue Jul 19 16:07:39 CDT 2011

Hi all

Now, would you believe this _old_ bug has survived A2010 and .Net 4 / Visual Studio 2010!

So - as someone brought up a long-winded solution - I had to post my version of the extension method for DateTime of C# at CodeProject:



>>> Gustav at cactus.dk 24-05-2007 16:44 >>>
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.
      ' Correct for Access97/2000 bug.
      bytWeek = cbytFirstWeekOfAnyYear
    End If
  End If
  ISO_WeekNumber = bytWeek

End Function


>>> 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?


Mark A. Matte

More information about the dba-VB mailing list