[AccessD] Count of a weekday between two dates

Gustav Brock Gustav at cactus.dk
Thu Apr 17 08:37:13 CDT 2008


Hi all

Recently I had to find the count of a weekday between two dates. Much to my surprise such code was not to be located.
I had an old function for this but found out, that the trick is to fool DateDiff to believe that the week starts on the weekday in question.

<code>
Public Function WeekdayDiff( _
  ByVal datDate1 As Date, _
  ByVal datDate2 As Date, _
  Optional ByVal bytWeekday As Byte) _
  As Long

  ' Returns signed count of a weekday of value bytWeekday
  ' between datDate1 and datDate2.
  '
  ' bytWeekday values should be one of the constants from vbMonday to vbSunday.
  ' If not or if not supplied, the weekday of datDate1 is assumed.
  '
  ' 2001-08-23. Cactus Data ApS, CPH.
  ' 2008-04-09. Rewrite using DateDiff.
  
  Dim lngWeekdayDiff  As Long
  
  ' No special error handling.
  On Error Resume Next
  
  ' Validate bytWeekday.
  Select Case bytWeekday
    Case _
      vbMonday, _
      vbTuesday, _
      vbWednesday, _
      vbThursday, _
      vbFriday, _
      vbSaturday, _
      vbSunday
    Case Else
      ' None or invalid value for weekday.
      bytWeekday = WeekDay(datDate1, vbSunday)
  End Select
  
  lngWeekdayDiff = DateDiff("ww", datDate1, datDate2, bytWeekday)
  
  WeekdayDiff = lngWeekdayDiff
    
End Function
</code>

Have fun!

/gustav





More information about the AccessD mailing list