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.

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.