CDate Function
Returns an expression that has been converted to a Variant of subtype Date.
Syntax
CDate(date)
Output date format is based on the system’s short date format.
Supported Date Formats:
- MM-DD-YYYY
- MM/DD/YYYY
- DD-MM-YYYY
- DD/MM/YYYY
- YYYY-MM-DD
- YYYY/MM/DD
Remarks
The date argument is any valid date expression.
Use the IsDate function to determine if date can be converted to a
date or time. CDate recognizes date literals and time literals as
well as some numbers that fall within the range of acceptable dates.
CDate recognizes date formats according to the locale setting of
your system. The correct order of day, month, and year may not be determined if it
is provided in a format other than one of the recognized date settings. In addition,
a long date format is not recognized if it also contains the day-of-the-week
string.
The following example illustrates the use of the CDate function:
Example
Dim MyDate1, MyDate2, MyDate3
MyDate1 = "19/10/1962" ' Define date in DD/MM/YYYY format.
MyDate2 = "10/19/1962" ' Define date in MM/DD/YYYY format.
MyDate3 = "1962-10-19" ' Define date in YYYY-MM-DD format.
Dim MyOutDate1, MyOutDate2, MyOutDate3
MyOutDate1 = CDate(MyDate1)
'Output: 19-Oct-62
MyOutDate2 = CDate(MyDate2)
'Output: 19-Oct-62
MyOutDate3 = CDate(MyDate3)
'Output: 19-Oct-62