<!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 - formatdatetime - String">
<title>formatdatetime - String - 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/string/index.html">String</a> / formatdatetime</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="formatdatetime-function">FormatDateTime Function</h1>
<p>Returns an expression formatted as a date or time.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">FormatDateTime(date[, namedformat])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>date</strong>: Required. Date expression to be formatted.</li>
<li><strong>namedformat</strong>: Optional. <code>Numeric</code> value that indicates the date/time format used. If omitted, <code>vbGeneralDate</code> is used.</li>
</ul>
<h2 id="named-format-constants">Named Format Constants</h2>
<ul>
<li><strong><code>vbGeneralDate</code></strong> (0): Display a date and/or time. For real numbers, display a date and time. If there is no fractional part, display only a date. If there is no integer part, display time only. Date and time display is determined by system settings.</li>
<li><strong><code>vbLongDate</code></strong> (1): Display a date using the long date format specified in the computer's regional settings.</li>
<li><strong><code>vbShortDate</code></strong> (2): Display a date using the short date format specified in the computer's regional settings.</li>
<li><strong><code>vbLongTime</code></strong> (3): Display a time using the time format specified in the computer's regional settings.</li>
<li><strong><code>vbShortTime</code></strong> (4): Display a time using the 24-hour format (hh:mm).</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Variant of subtype String containing the formatted date/time expression.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>FormatDateTime</code> function provides a simple way to format date and time values
using predefined formats based on the system's regional settings. It's easier to use
than Format for common date/time formatting needs.
<strong>Important Characteristics:</strong>
- Uses system locale for date/time formatting
- Only supports 5 predefined formats
- Less flexible than Format function
- Locale-aware (respects regional settings)
- Returns empty string if date is Null
- Date-only values show date without time
- Time-only values show time without date
- Format determined by Windows regional settings
- Cannot customize format patterns
- Easier for simple date/time display</p>
<h2 id="typical-uses">Typical Uses</h2>
<ul>
<li>Display dates in user's preferred format</li>
<li>Show timestamps in applications</li>
<li>Format dates for reports</li>
<li>Display file modification times</li>
<li>Show appointment times</li>
<li>Format log timestamps</li>
<li>Display birth dates</li>
<li>Show transaction dates</li>
</ul>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">Dim dt As Date
dt = #1/15/2025 3:45:30 PM#
' General date (default)
Debug.Print FormatDateTime(dt) ' 1/15/2025 3:45:30 PM
Debug.Print FormatDateTime(dt, vbGeneralDate) ' 1/15/2025 3:45:30 PM
' Long date
Debug.Print FormatDateTime(dt, vbLongDate) ' Wednesday, January 15, 2025
' Short date
Debug.Print FormatDateTime(dt, vbShortDate) ' 1/15/2025
' Long time
Debug.Print FormatDateTime(dt, vbLongTime) ' 3:45:30 PM
' Short time
Debug.Print FormatDateTime(dt, vbShortTime) ' 15:45</code></pre>
<h3 id="date-only">Date Only</h3>
<pre><code class="language-vbnet">Dim dateOnly As Date
dateOnly = #1/15/2025#
Debug.Print FormatDateTime(dateOnly, vbGeneralDate) ' 1/15/2025
Debug.Print FormatDateTime(dateOnly, vbLongDate) ' Wednesday, January 15, 2025
Debug.Print FormatDateTime(dateOnly, vbShortDate) ' 1/15/2025</code></pre>
<h3 id="time-only">Time Only</h3>
<pre><code class="language-vbnet">Dim timeOnly As Date
timeOnly = #3:45:30 PM#
Debug.Print FormatDateTime(timeOnly, vbGeneralDate) ' 3:45:30 PM
Debug.Print FormatDateTime(timeOnly, vbLongTime) ' 3:45:30 PM
Debug.Print FormatDateTime(timeOnly, vbShortTime) ' 15:45</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="display-current-datetime">Display Current Date/Time</h3>
<pre><code class="language-vbnet">Sub ShowCurrentDateTime()
Debug.Print "Current Date (Long): " & FormatDateTime(Now, vbLongDate)
Debug.Print "Current Date (Short): " & FormatDateTime(Now, vbShortDate)
Debug.Print "Current Time (Long): " & FormatDateTime(Now, vbLongTime)
Debug.Print "Current Time (Short): " & FormatDateTime(Now, vbShortTime)
End Sub</code></pre>
<h3 id="format-label-caption">Format Label Caption</h3>
<pre><code class="language-vbnet">Sub UpdateDateLabel()
lblCurrentDate.Caption = "Today is " & FormatDateTime(Date, vbLongDate)
lblCurrentTime.Caption = "Time: " & FormatDateTime(Time, vbLongTime)
End Sub</code></pre>
<h3 id="log-entry-formatting">Log Entry Formatting</h3>
<pre><code class="language-vbnet">Sub WriteLog(message As String)
Dim logEntry As String
Dim timestamp As String
timestamp = FormatDateTime(Now, vbGeneralDate)
logEntry = "[" & timestamp & "] " & message
Debug.Print logEntry
' Or write to file
End Sub</code></pre>
<h3 id="display-file-information">Display File Information</h3>
<pre><code class="language-vbnet">Sub ShowFileInfo(filePath As String)
Dim fileDate As Date
On Error GoTo ErrorHandler
fileDate = FileDateTime(filePath)
Debug.Print "File: " & filePath
Debug.Print "Modified: " & FormatDateTime(fileDate, vbLongDate) & _
" at " & FormatDateTime(fileDate, vbLongTime)
Exit Sub
ErrorHandler:
Debug.Print "Error reading file date"
End Sub</code></pre>
<h3 id="format-listbox-items">Format <code>ListBox</code> Items</h3>
<pre><code class="language-vbnet">Sub PopulateDateList(lstDates As ListBox, dates() As Date)
Dim i As Long
lstDates.Clear
For i = LBound(dates) To UBound(dates)
lstDates.AddItem FormatDateTime(dates(i), vbShortDate)
Next i
End Sub</code></pre>
<h3 id="display-birthday">Display Birthday</h3>
<pre><code class="language-vbnet">Function FormatBirthday(birthDate As Date) As String
Dim age As Integer
age = DateDiff("yyyy", birthDate, Date)
FormatBirthday = FormatDateTime(birthDate, vbLongDate) & " (Age: " & age & ")"
End Function
' Usage
Debug.Print FormatBirthday(#3/15/1990#)</code></pre>
<h3 id="format-appointment-display">Format Appointment Display</h3>
<pre><code class="language-vbnet">Function FormatAppointment(appointmentDate As Date, description As String) As String
Dim dateStr As String
Dim timeStr As String
dateStr = FormatDateTime(appointmentDate, vbLongDate)
timeStr = FormatDateTime(appointmentDate, vbShortTime)
FormatAppointment = description & vbCrLf & _
"Date: " & dateStr & vbCrLf & _
"Time: " & timeStr
End Function</code></pre>
<h3 id="display-relative-time">Display Relative Time</h3>
<pre><code class="language-vbnet">Function FormatRelativeDate(dt As Date) As String
Dim daysDiff As Long
daysDiff = DateDiff("d", dt, Date)
Select Case daysDiff
Case 0
FormatRelativeDate = "Today at " & FormatDateTime(dt, vbShortTime)
Case 1
FormatRelativeDate = "Yesterday at " & FormatDateTime(dt, vbShortTime)
Case -1
FormatRelativeDate = "Tomorrow at " & FormatDateTime(dt, vbShortTime)
Case Is > 1
FormatRelativeDate = FormatDateTime(dt, vbShortDate)
Case Is < -1
FormatRelativeDate = FormatDateTime(dt, vbShortDate)
End Select
End Function</code></pre>
<h3 id="format-database-display">Format Database Display</h3>
<pre><code class="language-vbnet">Function GetFormattedDate(rs As ADODB.Recordset, fieldName As String) As String
If IsNull(rs.Fields(fieldName).Value) Then
GetFormattedDate = "N/A"
Else
GetFormattedDate = FormatDateTime(rs.Fields(fieldName).Value, vbShortDate)
End If
End Function</code></pre>
<h3 id="create-date-range-display">Create Date Range Display</h3>
<pre><code class="language-vbnet">Function FormatDateRange(startDate As Date, endDate As Date) As String
FormatDateRange = FormatDateTime(startDate, vbShortDate) & " - " & _
FormatDateTime(endDate, vbShortDate)
End Function
' Usage
Debug.Print FormatDateRange(#1/1/2025#, #1/31/2025#) ' 1/1/2025 - 1/31/2025</code></pre>
<h3 id="format-grid-display">Format Grid Display</h3>
<pre><code class="language-vbnet">Sub PopulateTransactionGrid(grid As MSFlexGrid, transactions As Collection)
Dim i As Long
Dim trans As Variant
grid.Clear
grid.Rows = transactions.Count + 1
' Headers
grid.TextMatrix(0, 0) = "Date"
grid.TextMatrix(0, 1) = "Time"
grid.TextMatrix(0, 2) = "Description"
i = 1
For Each trans In transactions
grid.TextMatrix(i, 0) = FormatDateTime(trans.TransDate, vbShortDate)
grid.TextMatrix(i, 1) = FormatDateTime(trans.TransDate, vbShortTime)
grid.TextMatrix(i, 2) = trans.Description
i = i + 1
Next trans
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="flexible-date-formatter">Flexible Date Formatter</h3>
<pre><code class="language-vbnet">Function FormatDateEx(dt As Date, style As String) As String
Select Case LCase(style)
Case "long"
FormatDateEx = FormatDateTime(dt, vbLongDate)
Case "short"
FormatDateEx = FormatDateTime(dt, vbShortDate)
Case "longtime"
FormatDateEx = FormatDateTime(dt, vbLongTime)
Case "shorttime"
FormatDateEx = FormatDateTime(dt, vbShortTime)
Case "full"
FormatDateEx = FormatDateTime(dt, vbLongDate) & " " & _
FormatDateTime(dt, vbLongTime)
Case "compact"
FormatDateEx = FormatDateTime(dt, vbShortDate) & " " & _
FormatDateTime(dt, vbShortTime)
Case Else
FormatDateEx = FormatDateTime(dt, vbGeneralDate)
End Select
End Function</code></pre>
<h3 id="multi-format-display">Multi-Format Display</h3>
<pre><code class="language-vbnet">Sub DisplayAllFormats(dt As Date)
Debug.Print "Date/Time: " & dt
Debug.Print String(60, "=")
Debug.Print "General Date: ", FormatDateTime(dt, vbGeneralDate)
Debug.Print "Long Date: ", FormatDateTime(dt, vbLongDate)
Debug.Print "Short Date: ", FormatDateTime(dt, vbShortDate)
Debug.Print "Long Time: ", FormatDateTime(dt, vbLongTime)
Debug.Print "Short Time: ", FormatDateTime(dt, vbShortTime)
End Sub</code></pre>
<h3 id="calendar-event-formatter">Calendar Event Formatter</h3>
<pre><code class="language-vbnet">Type CalendarEvent
Title As String
StartTime As Date
EndTime As Date
AllDay As Boolean
End Type
Function FormatCalendarEvent(evt As CalendarEvent) As String
Dim result As String
result = evt.Title & vbCrLf
result = result & FormatDateTime(evt.StartTime, vbLongDate) & vbCrLf
If evt.AllDay Then
result = result & "All Day Event"
Else
result = result & FormatDateTime(evt.StartTime, vbShortTime) & " - " & _
FormatDateTime(evt.EndTime, vbShortTime)
End If
FormatCalendarEvent = result
End Function</code></pre>
<h3 id="report-header-generator">Report Header Generator</h3>
<pre><code class="language-vbnet">Function GenerateReportHeader(reportTitle As String) As String
Dim header As String
header = String(60, "=") & vbCrLf
header = header & reportTitle & vbCrLf
header = header & "Generated: " & FormatDateTime(Now, vbLongDate) & vbCrLf
header = header & "Time: " & FormatDateTime(Now, vbLongTime) & vbCrLf
header = header & String(60, "=") & vbCrLf
GenerateReportHeader = header
End Function</code></pre>
<h3 id="timestamp-logger">Timestamp Logger</h3>
<pre><code class="language-vbnet">Class TimestampLogger
Private logEntries As Collection
Private Sub Class_Initialize()
Set logEntries = New Collection
End Sub
Public Sub LogEvent(message As String, Optional includeTime As Boolean = True)
Dim entry As String
If includeTime Then
entry = FormatDateTime(Now, vbShortDate) & " " & _
FormatDateTime(Now, vbLongTime) & " - " & message
Else
entry = FormatDateTime(Now, vbShortDate) & " - " & message
End If
logEntries.Add entry
End Sub
Public Function GetLog() As String
Dim entry As Variant
Dim result As String
For Each entry In logEntries
result = result & entry & vbCrLf
Next entry
GetLog = result
End Function
End Class</code></pre>
<h3 id="smart-date-display">Smart Date Display</h3>
<pre><code class="language-vbnet">Function SmartFormatDate(dt As Date, Optional includeTime As Boolean = False) As String
Dim today As Date
Dim diff As Long
today = Date
diff = DateDiff("d", today, dt)
If diff = 0 Then
SmartFormatDate = "Today"
ElseIf diff = -1 Then
SmartFormatDate = "Yesterday"
ElseIf diff = 1 Then
SmartFormatDate = "Tomorrow"
ElseIf diff > -7 And diff < 0 Then
SmartFormatDate = Format(dt, "dddd") ' Day name
Else
SmartFormatDate = FormatDateTime(dt, vbShortDate)
End If
If includeTime Then
SmartFormatDate = SmartFormatDate & " at " & _
FormatDateTime(dt, vbShortTime)
End If
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeFormatDateTime(value As Variant, _
Optional style As VbDateTimeFormat = vbGeneralDate) As String
On Error GoTo ErrorHandler
If IsNull(value) Then
SafeFormatDateTime = "N/A"
ElseIf IsDate(value) Then
SafeFormatDateTime = FormatDateTime(CDate(value), style)
Else
SafeFormatDateTime = "Invalid Date"
End If
Exit Function
ErrorHandler:
Select Case Err.Number
Case 13 ' Type mismatch
SafeFormatDateTime = "Type Error"
Case 5 ' Invalid procedure call
SafeFormatDateTime = "Invalid Format"
Case Else
SafeFormatDateTime = "Error"
End Select
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 13</strong> (Type Mismatch): Value is not a valid date</li>
<li><strong>Error 5</strong> (Invalid procedure call): Invalid format constant</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>FormatDateTime</code> is fast for predefined formats</li>
<li>Faster than <code>Format</code> for simple date/time display</li>
<li>Locale lookups cached by system</li>
<li>Minimal overhead for formatting</li>
<li>Consider caching formatted strings for repeated display</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="use-formatdatetime-for-simple-formatting">Use <code>FormatDateTime</code> for Simple Formatting</h3>
<pre><code class="language-vbnet">' Good - Simple and locale-aware
formatted = FormatDateTime(dt, vbShortDate)
' Overkill for simple display
formatted = Format(dt, "mm/dd/yyyy")</code></pre>
<h3 id="handle-null-values">Handle Null Values</h3>
<pre><code class="language-vbnet">' Good - Check for Null
If Not IsNull(dateValue) Then
formatted = FormatDateTime(dateValue, vbShortDate)
Else
formatted = "N/A"
End If</code></pre>
<h3 id="use-appropriate-format-for-context">Use Appropriate Format for Context</h3>
<pre><code class="language-vbnet">' Good - Long date for formal display
lblEventDate.Caption = FormatDateTime(eventDate, vbLongDate)
' Good - Short date for compact display
txtDate.Text = FormatDateTime(Date, vbShortDate)</code></pre>
<h2 id="comparison-with-other-functions">Comparison with Other Functions</h2>
<h3 id="formatdatetime-vs-format"><code>FormatDateTime</code> vs <code>Format</code></h3>
<pre><code class="language-vbnet">' `FormatDateTime` - Predefined formats only
result = FormatDateTime(Now, vbLongDate)
' `Format` - Custom patterns possible
result = Format(Now, "dddd, mmmm d, yyyy")</code></pre>
<h3 id="formatdatetime-vs-datepart"><code>FormatDateTime</code> vs <code>DatePart</code></h3>
<pre><code class="language-vbnet">' FormatDateTime - Returns formatted string
result = FormatDateTime(Now, vbShortDate) ' "1/15/2025"
' DatePart - Returns numeric part
result = DatePart("m", Now) ' 1</code></pre>
<h3 id="formatdatetime-vs-cstrstr"><code>FormatDateTime</code> vs <code>CStr</code>/<code>Str</code></h3>
<pre><code class="language-vbnet">' FormatDateTime - Locale-aware formatting
result = FormatDateTime(Now, vbShortDate) ' "1/15/2025"
' CStr - Default conversion
result = CStr(Now) ' "1/15/2025 3:45:30 PM"</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Only 5 predefined formats available</li>
<li>Cannot customize format patterns</li>
<li>Uses system locale (cannot specify different locale)</li>
<li>Less flexible than <code>Format</code> function</li>
<li>No control over date/time separators</li>
<li>Cannot specify culture-specific formats</li>
<li><code>Format</code> style depends on Windows settings</li>
</ul>
<h2 id="regional-settings-impact">Regional Settings Impact</h2>
<p>The <code>FormatDateTime</code> function behavior varies by locale:</p>
<h3 id="vbshortdate">vbShortDate</h3>
<ul>
<li><strong>United States</strong>: 1/15/2025</li>
<li><strong>United Kingdom</strong>: 15/01/2025</li>
<li><strong>Germany</strong>: 15.01.2025</li>
<li><strong>Japan</strong>: 2025/01/15</li>
</ul>
<h3 id="vblongdate">vbLongDate</h3>
<ul>
<li><strong>United States</strong>: Wednesday, January 15, 2025</li>
<li><strong>France</strong>: mercredi 15 janvier 2025</li>
<li><strong>Germany</strong>: Mittwoch, 15. Januar 2025</li>
</ul>
<h3 id="vbshorttime">vbShortTime</h3>
<ul>
<li><strong>Most locales</strong>: 15:45 (24-hour format)</li>
</ul>
<h3 id="vblongtime">vbLongTime</h3>
<ul>
<li><strong>United States</strong>: 3:45:30 PM</li>
<li><strong>Europe</strong>: 15:45:30</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Format</code>: More flexible date/time formatting with custom patterns</li>
<li><code>FormatCurrency</code>: Format numbers as currency</li>
<li><code>FormatNumber</code>: Format numbers without currency symbol</li>
<li><code>FormatPercent</code>: Format numbers as percentages</li>
<li><code>DatePart</code>: Extract specific parts of a date</li>
<li><code>DateDiff</code>: Calculate difference between dates</li>
<li><code>CDate</code>: Convert expression to Date type</li>
<li><code>IsDate</code>: Check if expression can be converted to Date</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 String</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>