Share via


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

In addition to the individual functions that return part of a date — Year, Month, Day, and Weekday — Microsoft® Visual Basic® for Applications (VBA) includes the DatePart function, which can return any part of a date. Although it might 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 are writing code that might 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.

See Also

Working with Dates and Times | The Date Data Type | Getting the Current Date and Time | Formatting a Date | Date Delimiters | Assembling a Date | Adding and Subtracting Dates | Calculating Elapsed Time