Time Statement
Sets the system time.
Syntax
Time = time
Parts
- time: Required. Any numeric expression, string expression, or any combination that can represent a time.
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
- Error 5: Invalid procedure call or argument - occurs with invalid time format.
- Error 13: Type mismatch - occurs with incompatible data types.
Best Practices
- Always use error handling when setting system time.
- Validate time strings before assignment using
IsDate(). - Use
TimeSerialorTimeValuefor programmatic time construction.
See Also
Timefunction (retrieve current system time)Datestatement (set system date)Nowfunction (get current date and time)TimeSerialfunction (create time from components)TimeValuefunction (convert string to time)