VB6Parse / Library / Datetime / timevalue

VB6 Library Reference

VB6 TimeValue Function The TimeValue function returns a Variant (Date) containing the time value represented by a string.

Syntax

TimeValue(time)

Parameters

Returns

Returns a Variant of subtype Date containing the time value. The date portion is set to zero (December 30, 1899).

Remarks

The TimeValue function parses a string and returns the corresponding time value: - String input: Accepts time strings in various formats (e.g., "14:30", "2:30 PM", "23:59:59") - Date portion: Always returns zero date (12/30/1899) - Locale aware: Accepts time formats based on system locale - Numeric input: Accepts numeric values representing fractional days (e.g., 0.5 = 12:00 PM) - Null handling: Returns Null if input is Null - Error handling: Error 13 (Type mismatch) if string cannot be parsed as a time - No milliseconds: Only parses up to seconds - Companion to DateValue: Use DateValue for date-only parsing - Type returned: Returns Variant (Date), not a string

Accepted Time Formats

Invalid Inputs

Typical Uses

  1. Parse User Input: Convert time strings to time values
  2. Time Comparison: Compare parsed times to other time values
  3. Scheduling: Store and use times from configuration or user entry
  4. Time Calculations: Perform arithmetic with parsed times
  5. Validation: Check if input is a valid time
  6. Database Import: Parse time strings from data sources
  7. Time Filtering: Filter records by parsed time
  8. Time Formatting: Convert string to time for display or calculation

Basic Examples

Example 1: Parse Time String

Sub ParseTime()
    Dim t As Date
    t = TimeValue("14:30:00")
    MsgBox "Parsed time: " & Format$(t, "hh:mm AM/PM")
End Sub

Example 2: Parse 12-hour Time

Function ParseNoon() As Date
    ParseNoon = TimeValue("12:00 PM")
End Function

Example 3: Validate Time Input

Function IsValidTime(inputStr As String) As Boolean
    On Error Resume Next
    Dim t As Date
    t = TimeValue(inputStr)
    IsValidTime = (Err.Number = 0)
    On Error GoTo 0
End Function

Example 4: Compare Parsed Time

Function IsAfterNoon(timeStr As String) As Boolean
    IsAfterNoon = (TimeValue(timeStr) > TimeValue("12:00 PM"))
End Function

Common Patterns

Pattern 1: Parse and Add Minutes

Function AddMinutes(timeStr As String, minutes As Integer) As Date
    AddMinutes = TimeValue(timeStr) + TimeSerial(0, minutes, 0)
End Function

Pattern 2: Parse and Compare to Now

Function IsPast(timeStr As String) As Boolean
    IsPast = (TimeValue(timeStr) < Time)
End Function

Pattern 3: Parse and Format

Function FormatParsedTime(timeStr As String) As String
    FormatParsedTime = Format$(TimeValue(timeStr), "hh:mm:ss")
End Function

Pattern 4: Parse with Error Handling

Function TryParseTime(timeStr As String, ByRef result As Date) As Boolean
    On Error Resume Next
    result = TimeValue(timeStr)
    TryParseTime = (Err.Number = 0)
    On Error GoTo 0
End Function

Pattern 5: Parse Numeric Time

Function ParseNumericTime(numericValue As Double) As Date
    ParseNumericTime = TimeValue(numericValue)
End Function

Pattern 6: Parse and Set Property

Sub SetStartTime(obj As Object, timeStr As String)
    obj.StartTime = TimeValue(timeStr)
End Sub

Pattern 7: Parse Array of Times

Function ParseTimeArray(timeStrings() As String) As Variant
    Dim i As Integer
    Dim times() As Date
    ReDim times(LBound(timeStrings) To UBound(timeStrings))
    For i = LBound(timeStrings) To UBound(timeStrings)
        times(i) = TimeValue(timeStrings(i))
    Next i
    ParseTimeArray = times
End Function

Pattern 8: Filter Valid Times

Function FilterValidTimes(timeStrings() As String) As Collection
    Dim validTimes As New Collection
    Dim i As Integer
    For i = LBound(timeStrings) To UBound(timeStrings)
        If IsValidTime(timeStrings(i)) Then
            validTimes.Add TimeValue(timeStrings(i))
        End If
    Next i
    Set FilterValidTimes = validTimes
End Function

Pattern 9: Parse and Use in Schedule

Sub AddScheduleEntry(schedule As Collection, timeStr As String, description As String)
    Dim entry As New Collection
    entry.Add TimeValue(timeStr), "Time"
    entry.Add description, "Description"
    schedule.Add entry
End Sub

Pattern 10: Parse and Compare Range

Function IsWithinRange(timeStr As String, startStr As String, endStr As String) As Boolean
    Dim t As Date
    t = TimeValue(timeStr)
    IsWithinRange = (t >= TimeValue(startStr) And t <= TimeValue(endStr))
End Function

Advanced Usage

Example 1: Time Parser Class

' Class: TimeParser
' Parses and validates time strings
Option Explicit
Public Function Parse(timeStr As String) As Date
    Parse = TimeValue(timeStr)
End Function
Public Function TryParse(timeStr As String, ByRef result As Date) As Boolean
    On Error Resume Next
    result = TimeValue(timeStr)
    TryParse = (Err.Number = 0)
    On Error GoTo 0
End Function
Public Function IsValid(timeStr As String) As Boolean
    On Error Resume Next
    Dim t As Date
    t = TimeValue(timeStr)
    IsValid = (Err.Number = 0)
    On Error GoTo 0
End Function
Public Function Format(timeStr As String, formatStr As String) As String
    Format = Format$(TimeValue(timeStr), formatStr)
End Function

Example 2: Schedule Importer Module

' Module: ScheduleImporter
' Imports and parses schedule times from data
Option Explicit
Public Function ImportSchedule(times() As String, descriptions() As String) As Collection
    Dim schedule As New Collection
    Dim i As Integer
    For i = LBound(times) To UBound(times)
        Dim entry As New Collection
        entry.Add TimeValue(times(i)), "Time"
        entry.Add descriptions(i), "Description"
        schedule.Add entry
    Next i
    Set ImportSchedule = schedule
End Function
Public Function GetEarliestTime(times() As String) As Date
    Dim minTime As Date
    Dim i As Integer
    minTime = TimeValue(times(LBound(times)))
    For i = LBound(times) + 1 To UBound(times)
        If TimeValue(times(i)) < minTime Then minTime = TimeValue(times(i))
    Next i
    GetEarliestTime = minTime
End Function
Public Function GetLatestTime(times() As String) As Date
    Dim maxTime As Date
    Dim i As Integer
    maxTime = TimeValue(times(LBound(times)))
    For i = LBound(times) + 1 To UBound(times)
        If TimeValue(times(i)) > maxTime Then maxTime = TimeValue(times(i))
    Next i
    GetLatestTime = maxTime
End Function

Example 3: Time Validator Class

' Class: TimeValidator
' Validates and normalizes time strings
Option Explicit
Public Function IsValid(timeStr As String) As Boolean
    On Error Resume Next
    Dim t As Date
    t = TimeValue(timeStr)
    IsValid = (Err.Number = 0)
    On Error GoTo 0
End Function
Public Function Normalize(timeStr As String) As String
    On Error Resume Next
    Dim t As Date
    t = TimeValue(timeStr)
    If Err.Number = 0 Then
        Normalize = Format$(t, "hh:mm:ss")
    Else
        Normalize = ""
    End If
    On Error GoTo 0
End Function

Example 4: Time Range Analyzer Module

' Module: TimeRangeAnalyzer
' Analyzes and compares time ranges
Option Explicit
Public Function GetTimeRange(times() As String) As String
    Dim minTime As Date, maxTime As Date
    Dim i As Integer
    minTime = TimeValue(times(LBound(times)))
    maxTime = minTime
    For i = LBound(times) + 1 To UBound(times)
        If TimeValue(times(i)) < minTime Then minTime = TimeValue(times(i))
        If TimeValue(times(i)) > maxTime Then maxTime = TimeValue(times(i))
    Next i
    GetTimeRange = Format$(minTime, "hh:mm") & " - " & Format$(maxTime, "hh:mm")
End Function
Public Function IsWithinRange(timeStr As String, startStr As String, endStr As String) As Boolean
    Dim t As Date
    t = TimeValue(timeStr)
    IsWithinRange = (t >= TimeValue(startStr) And t <= TimeValue(endStr))
End Function

Error Handling

The TimeValue function can raise the following errors: - Error 13 (Type mismatch): If the string cannot be parsed as a time - Returns Null: If the input is Null (not an error)

Performance Notes

Best Practices

  1. Validate input before calling if user-provided
  2. Handle Null explicitly when working with nullable fields
  3. Use Format$ for display after parsing
  4. Test with different locales for internationalization
  5. Use TryParse pattern for robust error handling
  6. Document expected formats for maintainability
  7. Store as Date type for calculations
  8. Use with TimeSerial for arithmetic
  9. Test edge cases (midnight, noon, invalid times)
  10. Combine with DateValue for full date/time parsing

Comparison Table

Function Purpose Input Returns
TimeValue Parse time from string time string Date (time only)
DateValue Parse date from string date string Date (date only)
TimeSerial Create time from components hour, minute, second Date (time only)
DateSerial Create date from components year, month, day Date (date only)
CDate Convert to date expression Date

Platform Notes

Limitations

← Back to Datetime | View all functions