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
stringtimeis invalid, a runtime error is generated.If
stringtimeisNothing, TimeValue generates an error.stringtimecan 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.
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access