VB6Parse / Library / Datetime / time_dollar

VB6 Library Reference

Time$ Function

The Time$ function in Visual Basic 6 returns a string representing the current system time. The dollar sign ($) suffix indicates that this function always returns a String type, never a Variant.

Syntax

Time$

Parameters

None. Time$ takes no parameters.

Return Value

Returns a String representing the current system time in the format "HH:MM:SS" (24-hour format).

Behavior and Characteristics

Time Format

Type Differences: Time$ vs Time

System Time

Common Usage Patterns

1. Display Current Time

Sub ShowTime()
Debug.Print "Current time: " & Time$
End Sub

2. Timestamp for Logging

Sub LogMessage(message As String)
Dim logEntry As String
logEntry = Time$ & " - " & message
Debug.Print logEntry
End Sub

LogMessage "Application started"

3. Time-Based File Naming

Function GenerateTimeStampedFileName(baseName As String) As String
Dim timeStamp As String
timeStamp = Replace$(Time$, ":", "")
GenerateTimeStampedFileName = baseName & "_" & timeStamp & ".log"
End Function

' Generates: "logfile_143045.log" at 2:30:45 PM

4. Update Time Display

Sub Timer1_Timer()
' Update label every second
lblTime.Caption = Time$
End Sub

5. Record Processing Time

Sub ProcessData()
Dim startTime As String
startTime = Time$

' ... processing code ...

Debug.Print "Started at: " & startTime
Debug.Print "Completed at: " & Time$
End Sub

6. Time-Based Greetings

Function GetGreeting() As String
Dim currentHour As Integer
currentHour = Hour(Now)

If currentHour < 12 Then
GetGreeting = "Good morning! Time: " & Time$
ElseIf currentHour < 18 Then
GetGreeting = "Good afternoon! Time: " & Time$
Else
GetGreeting = "Good evening! Time: " & Time$
End If
End Function

7. Audit Trail Entries

Sub RecordAudit(action As String, userName As String)
Dim auditEntry As String
auditEntry = Date$ & " " & Time$ & " - " & userName & " - " & action
Print #1, auditEntry
End Sub

8. Periodic Task Checking

Sub CheckScheduledTask()
Dim currentTimeStr As String
currentTimeStr = Time$

If currentTimeStr = "09:00:00" Then
' Execute morning task
RunMorningReport
End If
End Sub

9. Status Bar Updates

Sub UpdateStatusBar()
StatusBar1.Panels(1).Text = "Current Time: " & Time$
End Sub

10. Debug Output with Timestamps

Sub DebugLog(category As String, message As String)
Debug.Print "[" & Time$ & "] " & category & ": " & message
End Sub

DebugLog "INFO", "Processing started"

Best Practices

When to Use Time$ vs Time vs Now

' Use Time$ for string display/logging
Debug.Print "Current time: " & Time$  ' "14:30:45"

' Use Time for time arithmetic
Dim currentTime As Date
currentTime = Time
Dim laterTime As Date
laterTime = currentTime + TimeSerial(1, 0, 0)  ' Add 1 hour

' Use Now for complete timestamp
Dim timestamp As Date
timestamp = Now  ' Includes both date and time

Combine with Date$ for Full Timestamp

Function GetFullTimestamp() As String
GetFullTimestamp = Date$ & " " & Time$
End Function

Debug.Print GetFullTimestamp()  ' "12/02/2025 14:30:45"

Use Format$ for Custom Time Formatting

' Time$ always returns 24-hour format
Debug.Print Time$  ' "14:30:45"

' Use Format$ for 12-hour format or other formats
Debug.Print Format$(Now, "hh:mm:ss AM/PM")  ' "02:30:45 PM"
Debug.Print Format$(Now, "Long Time")       ' "2:30:45 PM"

Replace Colons for File Names

Function SafeTimeStamp() As String
' Colons are invalid in filenames on Windows
SafeTimeStamp = Replace$(Time$, ":", "")
End Function

Dim fileName As String
fileName = "backup_" & SafeTimeStamp() & ".dat"  ' "backup_143045.dat"

Performance Considerations

' Less efficient: multiple calls
For i = 1 To 1000
Debug.Print Time$ & " - Item " & i
Next i

' More efficient: cache the time
Dim currentTime As String
currentTime = Time$
For i = 1 To 1000
Debug.Print currentTime & " - Item " & i
Next i

Common Pitfalls

1. 24-Hour Format Only

' Time$ always uses 24-hour format
Debug.Print Time$  ' "14:30:45" (not "2:30:45 PM")

' For 12-hour format, use Format$
Debug.Print Format$(Now, "hh:mm:ss AM/PM")  ' "02:30:45 PM"

2. String Comparison Issues

' Comparing time strings can be tricky
If Time$ = "9:00:00" Then  ' Will NEVER match!
' Time$ returns "09:00:00" with leading zero
End If

' Correct: include leading zero
If Time$ = "09:00:00" Then
' This works
End If

' Better: use Time value for comparisons
If Time > TimeSerial(9, 0, 0) Then
' More reliable
End If

3. Colons in File Names

' Invalid filename on Windows (colons not allowed)
fileName = "log_" & Time$ & ".txt"  ' "log_14:30:45.txt" - ERROR!

' Remove or replace colons
fileName = "log_" & Replace$(Time$, ":", "") & ".txt"  ' "log_143045.txt"
fileName = "log_" & Replace$(Time$, ":", "-") & ".txt"  ' "log_14-30-45.txt"

4. Time Changes During Execution

' Problem: time can change between calls
Debug.Print "Start: " & Time$
' ... long operation ...
Debug.Print "End: " & Time$  ' Different value!

' Solution: capture at start if consistency needed
Dim operationTime As String
operationTime = Time$
Debug.Print "Start: " & operationTime
' ... long operation ...
Debug.Print "Started at: " & operationTime
Debug.Print "Completed at: " & Time$

5. No Milliseconds

' Time$ only has second precision
Debug.Print Time$  ' "14:30:45" (no milliseconds)

' For higher precision, use Timer function
Dim startTimer As Single
startTimer = Timer
' ... operation ...
Debug.Print "Elapsed: " & (Timer - startTimer) & " seconds"

6. Locale Independence

' Time$ format is NOT affected by locale settings
' Always returns "HH:MM:SS" regardless of system locale
Debug.Print Time$  ' Always "14:30:45" format

' For locale-specific formatting, use Format$
Debug.Print Format$(Now, "Long Time")  ' Respects locale

Practical Examples

Creating Log Files with Timestamps

Sub WriteLog(message As String)
Dim logFile As String
Dim timeStamp As String

logFile = App.Path & "\application.log"
timeStamp = Date$ & " " & Time$

Open logFile For Append As #1
Print #1, timeStamp & " - " & message
Close #1
End Sub

Digital Clock Display

Private Sub tmrClock_Timer()
lblClock.Caption = Time$

' Update every second
tmrClock.Interval = 1000
End Sub

Performance Monitoring

Sub MonitorPerformance()
Dim startTime As Double
Dim endTime As Double

Debug.Print "Operation started at: " & Time$
startTime = Timer

' ... operation to monitor ...

endTime = Timer
Debug.Print "Operation ended at: " & Time$
Debug.Print "Elapsed time: " & (endTime - startTime) & " seconds"
End Sub

Scheduled Task Execution

Private Sub tmrScheduler_Timer()
Dim currentTimeStr As String
currentTimeStr = Time$

Select Case currentTimeStr
Case "09:00:00"
RunMorningReport
Case "12:00:00"
RunNoonBackup
Case "17:00:00"
RunEveningCleanup
End Select
End Sub

Limitations

← Back to Datetime | View all functions