<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - time_dollar - Datetime">
<title>time_dollar - Datetime - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/datetime/index.html">Datetime</a> / time_dollar</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="time-function">Time$ Function</h1>
<p>The <code>Time$</code> function in Visual Basic 6 returns a string representing the current system time.
The dollar sign (<code>$</code>) suffix indicates that this function always returns a <code>String</code> type,
never a <code>Variant</code>.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Time$</code></pre>
<h2 id="parameters">Parameters</h2>
<p>None. <code>Time$</code> takes no parameters.</p>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>String</code> representing the current system time in the format "HH:MM:SS" (24-hour format).</p>
<h2 id="behavior-and-characteristics">Behavior and Characteristics</h2>
<h3 id="time-format">Time Format</h3>
<ul>
<li>Always returns 24-hour format (e.g., "14:30:45" for 2:30:45 PM)</li>
<li>Format: "HH:MM:SS" where HH is 00-23, MM is 00-59, SS is 00-59</li>
<li>Always includes leading zeros (e.g., "09:05:03")</li>
<li>Does not include AM/PM indicator</li>
<li>Does not include milliseconds or fractional seconds</li>
</ul>
<h3 id="type-differences-time-vs-time">Type Differences: <code>Time$</code> vs <code>Time</code></h3>
<ul>
<li><code>Time$</code>: Always returns <code>String</code> type (never <code>Variant</code>)</li>
<li><code>Time</code>: Returns <code>Variant</code> containing a Date/Time value</li>
<li>Use <code>Time$</code> when you need a string representation</li>
<li>Use <code>Time</code> when you need to perform date/time arithmetic</li>
</ul>
<h3 id="system-time">System Time</h3>
<ul>
<li>Reflects the current system clock time</li>
<li>Updates each time the function is called</li>
<li>Accuracy depends on system clock resolution</li>
<li>No time zone information included</li>
</ul>
<h2 id="common-usage-patterns">Common Usage Patterns</h2>
<h3 id="1-display-current-time">1. Display Current Time</h3>
<pre><code class="language-vbnet">Sub ShowTime()
Debug.Print "Current time: " & Time$
End Sub</code></pre>
<h3 id="2-timestamp-for-logging">2. Timestamp for Logging</h3>
<pre><code class="language-vbnet">Sub LogMessage(message As String)
Dim logEntry As String
logEntry = Time$ & " - " & message
Debug.Print logEntry
End Sub
LogMessage "Application started"</code></pre>
<h3 id="3-time-based-file-naming">3. Time-Based File Naming</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="4-update-time-display">4. Update Time Display</h3>
<pre><code class="language-vbnet">Sub Timer1_Timer()
' Update label every second
lblTime.Caption = Time$
End Sub</code></pre>
<h3 id="5-record-processing-time">5. Record Processing Time</h3>
<pre><code class="language-vbnet">Sub ProcessData()
Dim startTime As String
startTime = Time$
' ... processing code ...
Debug.Print "Started at: " & startTime
Debug.Print "Completed at: " & Time$
End Sub</code></pre>
<h3 id="6-time-based-greetings">6. Time-Based Greetings</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="7-audit-trail-entries">7. Audit Trail Entries</h3>
<pre><code class="language-vbnet">Sub RecordAudit(action As String, userName As String)
Dim auditEntry As String
auditEntry = Date$ & " " & Time$ & " - " & userName & " - " & action
Print #1, auditEntry
End Sub</code></pre>
<h3 id="8-periodic-task-checking">8. Periodic Task Checking</h3>
<pre><code class="language-vbnet">Sub CheckScheduledTask()
Dim currentTimeStr As String
currentTimeStr = Time$
If currentTimeStr = "09:00:00" Then
' Execute morning task
RunMorningReport
End If
End Sub</code></pre>
<h3 id="9-status-bar-updates">9. Status Bar Updates</h3>
<pre><code class="language-vbnet">Sub UpdateStatusBar()
StatusBar1.Panels(1).Text = "Current Time: " & Time$
End Sub</code></pre>
<h3 id="10-debug-output-with-timestamps">10. Debug Output with Timestamps</h3>
<pre><code class="language-vbnet">Sub DebugLog(category As String, message As String)
Debug.Print "[" & Time$ & "] " & category & ": " & message
End Sub
DebugLog "INFO", "Processing started"</code></pre>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Time</code> - Returns a <code>Variant</code> containing the current system time (can be used in calculations)</li>
<li><code>Date$</code> - Returns a string representing the current system date</li>
<li><code>Now</code> - Returns the current system date and time as a <code>Date</code> value</li>
<li><code>Timer</code> - Returns the number of seconds elapsed since midnight</li>
<li><code>Hour()</code> - Extracts the hour component from a time value</li>
<li><code>Minute()</code> - Extracts the minute component from a time value</li>
<li><code>Second()</code> - Extracts the second component from a time value</li>
<li><code>Format$()</code> - Formats time values with custom formatting</li>
<li><code>TimeSerial()</code> - Creates a time value from hour, minute, and second</li>
<li><code>TimeValue()</code> - Converts a string to a time value</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="when-to-use-time-vs-time-vs-now">When to Use <code>Time$</code> vs <code>Time</code> vs <code>Now</code></h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="combine-with-date-for-full-timestamp">Combine with <code>Date$</code> for Full Timestamp</h3>
<pre><code class="language-vbnet">Function GetFullTimestamp() As String
GetFullTimestamp = Date$ & " " & Time$
End Function
Debug.Print GetFullTimestamp() ' "12/02/2025 14:30:45"</code></pre>
<h3 id="use-format-for-custom-time-formatting">Use <code>Format$</code> for Custom Time Formatting</h3>
<pre><code class="language-vbnet">' 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"</code></pre>
<h3 id="replace-colons-for-file-names">Replace Colons for File Names</h3>
<pre><code class="language-vbnet">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"</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Time$</code> is a system call and relatively fast</li>
<li>Calling repeatedly in tight loops may impact performance</li>
<li>Cache the value if you need it multiple times in the same operation</li>
</ul>
<pre><code class="language-vbnet">' 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</code></pre>
<h2 id="common-pitfalls">Common Pitfalls</h2>
<h3 id="1-24-hour-format-only">1. 24-Hour Format Only</h3>
<pre><code class="language-vbnet">' 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"</code></pre>
<h3 id="2-string-comparison-issues">2. String Comparison Issues</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="3-colons-in-file-names">3. Colons in File Names</h3>
<pre><code class="language-vbnet">' 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"</code></pre>
<h3 id="4-time-changes-during-execution">4. Time Changes During Execution</h3>
<pre><code class="language-vbnet">' 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$</code></pre>
<h3 id="5-no-milliseconds">5. No Milliseconds</h3>
<pre><code class="language-vbnet">' 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"</code></pre>
<h3 id="6-locale-independence">6. Locale Independence</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h2 id="practical-examples">Practical Examples</h2>
<h3 id="creating-log-files-with-timestamps">Creating Log Files with Timestamps</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="digital-clock-display">Digital Clock Display</h3>
<pre><code class="language-vbnet">Private Sub tmrClock_Timer()
lblClock.Caption = Time$
' Update every second
tmrClock.Interval = 1000
End Sub</code></pre>
<h3 id="performance-monitoring">Performance Monitoring</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="scheduled-task-execution">Scheduled Task Execution</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Always returns 24-hour format (no AM/PM)</li>
<li>No millisecond or sub-second precision</li>
<li>No time zone information</li>
<li>Format cannot be customized (use <code>Format$</code> for that)</li>
<li>Returns string, not suitable for time arithmetic (use <code>Time</code> function instead)</li>
<li>Colons in output make it unsuitable for filenames without modification</li>
<li>Cannot be set (read-only; use <code>Time =</code> statement to set system time)</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Datetime</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>