CDate Function Example

This example uses the CDate function to convert a string to a Date. In general, hard-coding dates and times as strings (as shown in this example) is not recommended. Use date literals and time literals, such as #2/12/1969# and #4:45:23 PM#, instead.

Dim MyDate, MyShortDate, MyTime, MyShortTime
MyDate = "February 12, 1969"    ' Define date.
MyShortDate = CDate(MyDate)    ' Convert to Date data type.

MyTime = "4:35:47 PM"    ' Define time.
MyShortTime = CDate(MyTime)    ' Convert to Date data type.