VB6Parse / Library / Runtime State / time

VB6 Library Reference

Time Statement

Sets the system time.

Syntax

Time = time

Parts

Remarks

Common Uses

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 to Noon

Time = "12: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 Using Current Time Plus Offset

Time = Time + TimeValue("00:15:00")  ' Add 15 minutes

Set Time from User Input

Dim userTime As String
userTime = InputBox("Enter new time (HH:MM:SS):")
If IsDate(userTime) Then
Time = userTime
Else
MsgBox "Invalid time format"
End If

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

Set Time Using Now Function

Time = Now  ' Sets time to current time (redundant but valid)

Set Time in Sub

Sub SetApplicationTime()
Time = "08:00:00"  ' Set to 8 AM
End Sub

Set Time Conditionally

If Hour(Time) > 17 Then
Time = "08:00:00"  ' Reset to morning
End If

Set Time Using TimeSerial

Time = TimeSerial(14, 30, 0)  ' 2:30 PM

Set Time with Concatenation

Dim hours As String
Dim minutes As String
hours = "09"
minutes = "45"
Time = hours & ":" & minutes & ":00"

Set Time for Testing

' Set specific time for testing time-dependent code
Time = "23:59:59"  ' One second before midnight
TestMidnightRollover

Set Time in Class Module

Private Sub Class_Initialize()
Time = "12:00:00"  ' Reset to noon on initialization
End Sub

Set Time Using Format

Dim timeStr As String
timeStr = Format(Now, "hh:mm:ss")
Time = timeStr

Set Time in Loop

For i = 0 To 23
Time = TimeSerial(i, 0, 0)
ProcessHourlyTask
Next i

Set Time with Validation

Function SetSystemTime(newTime As String) As Boolean
On Error GoTo ErrorHandler

If IsDate(newTime) Then
Time = newTime
SetSystemTime = True
Else
SetSystemTime = False
End If

Exit Function

ErrorHandler:
SetSystemTime = False
End Function

Important Notes

Time Formats Accepted

Common Errors

Best Practices

See Also

References

← Back to Runtime State | View all statements