O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

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

Name

TimeValue Function

Class

Microsoft.VisualBasic.DateAndTime

Syntax

TimeValue(stringtime)
stringtime (required; String)

Any valid string representation of a time

Return Value

A Date containing the time specified by the string argument, with the date set to January 1 of the year 1

Description

Converts a string representation of a time to a Date data type

Rules at a Glance

  • If stringtime is invalid, a runtime error is generated.

  • If stringtime is Nothing, TimeValue generates an error.

  • stringtime can be in any time format recognized by the IsDate function. Both 12- and 24-hour clock formats are valid.

  • The Date value returned by time is formatted based on the system’s regional settings.

Programming Tips and Gotchas

  • A time literal can also be assigned to a Date variable by surrounding the date with hash characters (#), as the following snippet demonstrates:

    Dim dMyTime As Date
    dMyTime = #12:30:00 AM#
  • The CDate function can also cast a time expression contained within a string as a Date variable, with the additional advantage of being internationally aware.

  • The string returned by the TimeString property also includes an invalid date, 01/01/0001. It can be eliminated with the Format or FormatDateTime function as follows:

    Format(TimeOfDay(  ), "Long Time")
    FormatDateTime(TimeOfDay(  ), DateFormat.LongTime)

VB/NET/VB 6 Differences

In VB 6, TimeValue returns the time only. In VB.NET, the function also returns an invalid date, 01/01/0001, along with the time.

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