O'Reilly logo

VB & VBA in a Nutshell: The Language by Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CDate Function

Named Arguments

No

Syntax

CDate(expression)

expression

Use: Required

Data Type: String or Numeric

Any valid date expression.

Return Value

expression converted into a Date data type.

Description

Converts expression to a Date data type. The format of expression—the order of day, month, and year—is determined by the locale setting of your computer. To be certain of a date being recognized correctly by CDate, the month, day, and year elements of expression must be in the same sequence as your computer's regional settings; otherwise the CDate function has no idea that 4 is supposed to be the 4th of the month, not the month of April.

CDate also converts numbers to a date. The precise behavior of the function, however, depends on the value of expression :

  • If expression is less than or equal to 23 and includes a fractional component less than 60, the integer is interpreted as the number of hours since midnight, and the fraction is interpreted as the number of seconds.

  • In all other cases, the integer portion of expression is converted to a date that interprets the integer as the number of days before (in the case of negative numbers) or after December 31, 1899, and its fractional part is converted to the time of day, with every .01 representing 864 seconds (14 minutes 24 seconds) after midnight.

Rules at a Glance

  • CDate accepts both numerical date expressions and string literals. You can pass month names into CDate in either complete or abbreviated form; for example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required