CDate Function

   
CDate Function

Named Arguments

No

Syntax

 CDate(   expression   ) 
expression (required; String or Numeric)

Any valid representation of a date and time

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 the local computer. To be certain a date is recognized correctly by CDate , the month, day, and year elements of expression must be in the same sequence as the local computer's regional settings; otherwise , the CDate function has no idea, for example, that 4 was supposed to be the fourth day of the month, not the month of April.

Rules at a Glance

  • You can use any of the date delimiters specified in your computer's regional settings; for most systems, this includes , , / , - , and .

  • The earliest date that can be handled by the Date data type is 01/01/100. The latest date that can be handled by the Date data type is 12/31/9999.

Programming Tips and Gotchas

  • Use the IsDate function to determine if expression can be converted to a date or time.

  • If you pass an empty string to CDate , an error is generated.

  • A modicum of intelligence has been built into the CDate function. It can determine the day and month from a string, regardless of their position in the string; this applies only where the day number is larger than 12, which automatically distinguishes it from the number of the month. For example, if the string "30/12/97" is passed into the CDate function on a system expecting a date format of mm/dd/yy , CDate sees that 30 is too large to represent a month and thus treats it as the day. This can lead to problems because if we accidentally pass a string such as "30/12/97" instead of the intended "3/12/ 97," then VB does not issue an error message!

  • If we pass a string whose year specification is less than three characters in length, then VB interprets the year as belonging to the twenty-first century. For instance, the string "1/1/1" is interpreted as "1/1/2001."

  • If you do not specify a year, the CDate function uses the year from the current date on your computer.

  • Like most conversion functions, CDate is not actually a function in the Microsoft. VisualBasic namespace. Instead, it is similar to a Visual C++ macro; the compiler translates the function call into inline code.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net