VB6Runtime / Library / Datetime / time_statement

VB6 Library Reference

Time Statement

Sets the system time.

Syntax

Time = time

Parts

Return Value

None. This is a statement, not a function.

Remarks

The Time statement sets the system time to the value of time. In VB6 on Windows this modifies the real system clock and requires administrator privileges. In this runtime the behavior depends on the allow_system_clock flag: - Default (system clock allowed): The time is written to the real system clock via platform APIs. Subsequent Time and Time$ calls return the real system time. - Internal-only mode: The time is stored in an offset-based mock clock that advances in real time from the set point. The real system clock is never modified. - Time Format: Accepts times in various formats including "HH:MM:SS", "HH:MM", or numeric values representing time. - 24-Hour Format: You can use 24-hour format (e.g., "13:30" for 1:30 PM) or 12-hour format with AM/PM. - Current Date Preserved: Setting the time does not affect the system date. - Time Function: Use the Time function (without assignment) to retrieve the current system time. - Error Handling: Invalid time values will generate a run-time error.

Examples

Set Time to Specific Hour and Minute

Time = "14:30:00"  ' Set to 2:30 PM

Set Time Using String

Time = "9:15 AM"

Set Time to Midnight

Time = "00:00:00"

Set Time Using Variable

Dim newTime As String
newTime = "15:45:30"
Time = newTime

Set Time Using TimeValue Function

Time = TimeValue("3:30 PM")

Set Time with Error Handling

On Error Resume Next
Time = "10:30:00"
If Err.Number <> 0 Then
    MsgBox "Failed to set time: " & Err.Description
End If
On Error GoTo 0

Common Errors

Best Practices

See Also

References

← Back to Datetime | View all statements