CDate Function

Description

Converts an expression to a Date.

Syntax

CDate(date)

The date argument is any valid date expression.

Remarks

Use the IsDate function to determine if date can be converted to a date or time. CDate recognizes date and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.

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.

Note A CVDate function is also provided for compatibility with previous versions of Visual Basic. However, since there is now an intrinsic Date type, there is no further need for CVDate. The syntax of the CVDate function is identical to the CDate function. The difference is that it returns a Variant whose subtype is Date instead of an actual Date type. The same effect can be achieved by converting an expression to a Date and then assigning it to a Variant. This technique is consistent with the conversion of all other intrinsic types to their equivalent Variant subtypes.

See Also

Data Type Summary, IsDate 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 and time literals (such as #2/12/1969#, #4:45:23 PM#) instead.


MyDate = "February 12, 1969"            ' Define date.= CDate(MyDate)            ' Convert to Date data type.= "4:35:47 PM"                ' Define time.= CDate(MyTime)            ' Convert to Date data type.