Microsoft Office 2000/Visual Basic Programmer's Guide   

Getting Part of a Date

The previous section showed how to return the year, month, and day from a date. You can get other information about a date as well, such as what quarter or week it falls in, or what day of the week it is.

The Weekday function takes a date and returns a constant indicating on what day of the week it falls. The following procedure takes a date and returns True if the date falls on a workday—that is, Monday through Friday—and False if it falls on a weekend.

Function IsWorkday(Optional dteDate As Date) As Boolean
   ' This function determines whether a date
   ' falls on a weekday.
   
   ' If no date passed in, use today's date.
   If CLng(dteDate) = 0 Then
      dteDate = Date
   End If
   
   ' Determine where in week the date falls.
   Select Case Weekday(dteDate)
      Case vbMonday To vbFriday
         IsWorkday = True
      Case Else
         IsWorkday = False
   End Select
End Function

This procedure is available in the modDateTime module in VBA.mdb in the ODETools\V9\Samples\OPG\Samples\CH07 subfolder on the Office 2000 Developer CD-ROM.

In addition to the individual functions that return part of a date—Year, Month, Day, and Weekday—VBA includes the DatePart function, which can return any part of a date. Although it may seem redundant, the DatePart function gives you slightly more control over the values you return, because it gives you the option to specify the first day of the week and the first day of the year. For this reason, it can be useful when you're writing code that may run on systems in other countries. In addition, the DatePart function is the only way to return information about what quarter a date falls into.