DateValue Function

   
DateValue Function

Class

Microsoft.VisualBasic.DateAndTime

Syntax

 DateValue(   stringdate   ) 
stringdate (required; String)

A string containing any of the date formats recognized by IsDate

Return Value

A Date that represents the date specified by the stringdate argument

Description

Returns a Date containing the date represented by stringdate .

The date value is formatted according to the short date setting defined by the Regional Settings applet in the Control Panel. DateValue can successfully recognize a stringdate in any of the date formats recognized by IsDate . DateValue does not return time values in a date/time string; they are simply dropped. However, if stringdate includes a valid date value but an invalid time value, a runtime error results.

Rules at a Glance

  • The order of the day, month, and year within stringdate must be the same as the sequence defined by the computer's regional settings.

  • Only those date separators recognized by IsDate can be used.

  • If you don't specify a year in your date expression, DateValue uses the current year from the computer's system date.

Example

 Dim sDateExpression As String sDateExpression = 10 & "/" & "March" & "/" & 1998 If IsDate(sDateExpression) Then     Console.WriteLine(DateValue(sDateExpression)) Else     Console.WriteLine("invalid date") End If 

Programming Tips and Gotchas

  • When working with dates, always check that a date is valid using the IsDate function prior to passing it as a function argument.

  • If stringdate includes time information as well as date information, the time information is ignored; however, if only time information is passed to DateValue , an error is generated.

  • DateValue handles two-digit years in the following manner: Year arguments between 0 and 29 are taken to be in the 21st century (2000 to 2029), while year arguments between 30 and 99 are taken to be in the 20th century (1930 to 1999). Of course, the safest way to specify a year is to use the full four digits.

  • On Windows NT/2000 systems, the date formats are held as string values in the following registry keys:

    Date Separator

    HKEY_CURRENT_USER\Control Panel\International, sDate value entry

    Long Date

    HKEY_CURRENT_USER\Control Panel\International, sLongDate value entry

    Short Date

    HKEY_CURRENT_USER\Control Panel\International, sShortDate value entry

  • The more common approach to date conversion is to use the CDate function. Microsoft also recommends using the C... conversion functions due to their enhanced capabilities and their locale awareness.

See Also

DatePart Function, DateSerial Function, DateString Property

   


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