<!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 - datevalue - Datetime">
<title>datevalue - 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> / datevalue</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="datevalue-function">DateValue Function</h1>
<p>Returns a <code>Variant</code> (<code>Date</code>) containing the date represented by a string expression.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">DateValue(date)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>date</strong>: Required. <code>String</code> expression representing a date from January 1, 100 through
December 31, 9999. Can also be any expression that can represent a date, a time, or
both a date and time, in that range.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Variant</code> of subtype <code>Date</code>. If the string includes valid time information, it's
not returned as part of the date (time is set to midnight). Returns <code>Null</code> if the string
cannot be converted to a valid date.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>DateValue</code> function is used to convert string representations of dates into actual
<code>Date</code> values. It recognizes dates according to the system locale settings.
<strong>Important Characteristics:</strong>
- Interprets strings according to system locale settings
- Recognizes various date formats (MM/DD/YYYY, Month DD, YYYY, etc.)
- Strips time information if present (returns date portion only)
- Two-digit years: 0-29 → 2000-2029, 30-99 → 1930-1999
- Month names can be spelled out or abbreviated
- Accepts dates in various formats depending on locale
- Returns midnight (00:00:00) for time portion
- Case-insensitive for month names</p>
<h2 id="recognized-date-formats">Recognized Date Formats</h2>
<p><code>DateValue</code> recognizes many formats (locale-dependent):</p>
<pre><code class="language-vbnet">' Numeric formats
DateValue("1/15/2025") ' MM/DD/YYYY (US)
DateValue("15/1/2025") ' DD/MM/YYYY (UK)
DateValue("2025-01-15") ' ISO format
DateValue("1-15-2025") ' With dashes
' Text formats
DateValue("January 15, 2025") ' Full month name
DateValue("Jan 15, 2025") ' Abbreviated month
DateValue("15 January 2025") ' Different order
DateValue("15-Jan-2025") ' Mixed format
' Short formats
DateValue("1/15/25") ' Two-digit year
DateValue("Jan 15") ' Assumes current year</code></pre>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">' Convert string to date
Dim birthday As Date
birthday = DateValue("5/15/1990")
MsgBox birthday
' With month name
Dim holiday As Date
holiday = DateValue("December 25, 2025")
' ISO format
Dim isoDate As Date
isoDate = DateValue("2025-01-15")</code></pre>
<h3 id="parse-user-input">Parse User Input</h3>
<pre><code class="language-vbnet">Function ParseDate(userInput As String) As Variant
On Error Resume Next
ParseDate = DateValue(userInput)
If Err.Number <> 0 Then
ParseDate = Null
End If
End Function
' Usage
Dim inputDate As Variant
inputDate = ParseDate(txtDate.Text)
If IsNull(inputDate) Then
MsgBox "Invalid date format"
End If</code></pre>
<h3 id="strip-time-from-datetime">Strip Time from <code>DateTime</code></h3>
<pre><code class="language-vbnet">Function GetDateOnly(dateTime As Variant) As Date
' Convert to string and back to strip time
GetDateOnly = DateValue(CStr(dateTime))
End Function
' Alternative using Int
Function GetDateOnly2(dateTime As Date) As Date
GetDateOnly2 = Int(dateTime)
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="validate-date-string">Validate Date String</h3>
<pre><code class="language-vbnet">Function IsValidDateString(dateStr As String) As Boolean
On Error Resume Next
Dim testDate As Date
testDate = DateValue(dateStr)
IsValidDateString = (Err.Number = 0)
End Function
' Usage
If IsValidDateString(txtDate.Text) Then
MsgBox "Valid date"
Else
MsgBox "Invalid date"
End If</code></pre>
<h3 id="parse-various-formats">Parse Various Formats</h3>
<pre><code class="language-vbnet">Function TryParseDateFormats(dateStr As String) As Variant
Dim formats As Variant
Dim i As Integer
formats = Array("MM/DD/YYYY", "DD/MM/YYYY", "YYYY-MM-DD", _
"Month DD, YYYY", "DD Month YYYY")
On Error Resume Next
TryParseDateFormats = DateValue(dateStr)
If Err.Number = 0 Then Exit Function
' Try with current year if not specified
TryParseDateFormats = DateValue(dateStr & " " & Year(Date))
End Function</code></pre>
<h3 id="import-data-with-date-parsing">Import Data with Date Parsing</h3>
<pre><code class="language-vbnet">Sub ImportDataWithDates(filePath As String)
Dim line As String
Dim fields() As String
Dim recordDate As Date
Open filePath For Input As #1
Do Until EOF(1)
Line Input #1, line
fields = Split(line, ",")
On Error Resume Next
recordDate = DateValue(fields(0))
If Err.Number = 0 Then
' Process valid date record
ProcessRecord recordDate, fields
Else
' Log invalid date
LogError "Invalid date: " & fields(0)
End If
On Error GoTo 0
Loop
Close #1
End Sub</code></pre>
<h3 id="date-range-validator">Date Range Validator</h3>
<pre><code class="language-vbnet">Function ValidateDateRange(startStr As String, endStr As String) As Boolean
Dim startDate As Date
Dim endDate As Date
On Error Resume Next
startDate = DateValue(startStr)
If Err.Number <> 0 Then Exit Function
endDate = DateValue(endStr)
If Err.Number <> 0 Then Exit Function
ValidateDateRange = (startDate <= endDate)
End Function</code></pre>
<h3 id="convert-text-file-dates">Convert Text File Dates</h3>
<pre><code class="language-vbnet">Function ConvertTextDate(textDate As String) As Date
' Convert various text formats to standard date
ConvertTextDate = DateValue(textDate)
End Function
Sub ProcessLogFile()
Dim logDate As Date
Dim dateStr As String
dateStr = "Jan 15, 2025"
logDate = ConvertTextDate(dateStr)
MsgBox Format(logDate, "yyyy-mm-dd")
End Sub</code></pre>
<h3 id="flexible-date-parser">Flexible Date Parser</h3>
<pre><code class="language-vbnet">Function SmartDateParse(input As String) As Variant
Dim result As Variant
On Error Resume Next
' Try as-is
result = DateValue(input)
If Err.Number = 0 Then
SmartDateParse = result
Exit Function
End If
Err.Clear
' Try adding current year
result = DateValue(input & ", " & Year(Date))
If Err.Number = 0 Then
SmartDateParse = result
Exit Function
End If
' Return Null if unparseable
SmartDateParse = Null
End Function</code></pre>
<h3 id="database-date-import">Database Date Import</h3>
<pre><code class="language-vbnet">Sub ImportDatabaseDates(rs As ADODB.Recordset)
Dim dateField As String
Dim parsedDate As Date
Do Until rs.EOF
dateField = rs("DateField").Value
On Error Resume Next
parsedDate = DateValue(dateField)
If Err.Number = 0 Then
' Update with parsed date
rs("DateField") = parsedDate
rs.Update
End If
rs.MoveNext
Loop
End Sub</code></pre>
<h3 id="form-date-validation">Form Date Validation</h3>
<pre><code class="language-vbnet">Function ValidateFormDate(ByRef txt As TextBox, fieldName As String) As Boolean
Dim testDate As Date
On Error Resume Next
testDate = DateValue(txt.Text)
If Err.Number <> 0 Then
MsgBox "Invalid " & fieldName & " format", vbExclamation
txt.SetFocus
ValidateFormDate = False
Else
ValidateFormDate = True
End If
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="multi-locale-date-parser">Multi-Locale Date Parser</h3>
<pre><code class="language-vbnet">Function ParseInternationalDate(dateStr As String, locale As String) As Variant
' This is simplified - VB6 doesn't have full locale switching
' Would need Windows API for true locale switching
On Error Resume Next
Select Case UCase(locale)
Case "US"
' Try MM/DD/YYYY first
ParseInternationalDate = DateValue(dateStr)
Case "UK", "EU"
' Parse with assumption of DD/MM/YYYY
' Would need custom parsing logic
ParseInternationalDate = DateValue(dateStr)
Case "ISO"
' YYYY-MM-DD format
ParseInternationalDate = DateValue(dateStr)
Case Else
ParseInternationalDate = DateValue(dateStr)
End Select
If Err.Number <> 0 Then
ParseInternationalDate = Null
End If
End Function</code></pre>
<h3 id="batch-date-conversion">Batch Date Conversion</h3>
<pre><code class="language-vbnet">Function ConvertDateArray(dateStrings() As String) As Variant
Dim dates() As Date
Dim i As Long
Dim validCount As Long
ReDim dates(LBound(dateStrings) To UBound(dateStrings))
validCount = 0
For i = LBound(dateStrings) To UBound(dateStrings)
On Error Resume Next
dates(i) = DateValue(dateStrings(i))
If Err.Number = 0 Then
validCount = validCount + 1
End If
Err.Clear
Next i
ConvertDateArray = dates
End Function</code></pre>
<h3 id="date-string-normalizer">Date String Normalizer</h3>
<pre><code class="language-vbnet">Function NormalizeDateString(input As String, outputFormat As String) As String
Dim parsedDate As Date
On Error Resume Next
parsedDate = DateValue(input)
If Err.Number = 0 Then
NormalizeDateString = Format(parsedDate, outputFormat)
Else
NormalizeDateString = ""
End If
End Function
' Usage: Convert various formats to ISO
Dim normalized As String
normalized = NormalizeDateString("Jan 15, 2025", "yyyy-mm-dd") ' Returns "2025-01-15"</code></pre>
<h3 id="excel-date-converter">Excel Date Converter</h3>
<pre><code class="language-vbnet">Function ExcelDateToVBDate(excelDateStr As String) As Variant
' Excel stores dates as numbers, but when exported may be text
Dim dateVal As Variant
On Error Resume Next
' Try as text date
dateVal = DateValue(excelDateStr)
If Err.Number <> 0 Then
Err.Clear
' Try as Excel serial number
If IsNumeric(excelDateStr) Then
dateVal = CDate(CDbl(excelDateStr))
Else
dateVal = Null
End If
End If
ExcelDateToVBDate = dateVal
End Function</code></pre>
<h3 id="calendar-date-picker-helper">Calendar Date Picker Helper</h3>
<pre><code class="language-vbnet">Function ParseCalendarInput(input As String) As Variant
' Handle various calendar input formats
Dim result As Date
On Error Resume Next
' Remove extra whitespace
input = Trim(input)
' Try direct conversion
result = DateValue(input)
If Err.Number = 0 Then
ParseCalendarInput = result
Exit Function
End If
' Try common substitutions
If LCase(input) = "today" Then
ParseCalendarInput = Date
ElseIf LCase(input) = "yesterday" Then
ParseCalendarInput = Date - 1
ElseIf LCase(input) = "tomorrow" Then
ParseCalendarInput = Date + 1
Else
ParseCalendarInput = Null
End If
End Function</code></pre>
<h3 id="report-date-filter">Report Date Filter</h3>
<pre><code class="language-vbnet">Function BuildDateFilter(fromStr As String, toStr As String) As String
Dim fromDate As Date
Dim toDate As Date
On Error Resume Next
fromDate = DateValue(fromStr)
toDate = DateValue(toStr)
If Err.Number = 0 Then
BuildDateFilter = "DateField >= #" & fromDate & "# AND DateField <= #" & toDate & "#"
Else
BuildDateFilter = ""
End If
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeDateValue(dateStr As String) As Variant
On Error GoTo ErrorHandler
' Validate input
If Len(Trim(dateStr)) = 0 Then
SafeDateValue = Null
Exit Function
End If
SafeDateValue = DateValue(dateStr)
Exit Function
ErrorHandler:
SafeDateValue = Null
End Function
Function SafeDateValueWithDefault(dateStr As String, defaultDate As Date) As Date
On Error Resume Next
SafeDateValueWithDefault = DateValue(dateStr)
If Err.Number <> 0 Then
SafeDateValueWithDefault = defaultDate
End If
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 13</strong> (Type mismatch): <code>String</code> cannot be recognized as a date</li>
<li><strong>Error 5</strong> (Invalid procedure call): <code>Date</code> is outside valid range</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>DateValue</code> involves string parsing, slower than <code>DateSerial</code></li>
<li>Use <code>DateSerial</code> when constructing dates from numeric components</li>
<li>Cache parsed dates when processing large datasets</li>
<li>Locale detection adds overhead</li>
<li>For known formats, custom parsing may be faster</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="always-validate-user-input">Always Validate User Input</h3>
<pre><code class="language-vbnet">' Good - Validate before use
On Error Resume Next
userDate = DateValue(txtInput.Text)
If Err.Number <> 0 Then
MsgBox "Please enter a valid date"
Exit Sub
End If
' Avoid - Assuming input is valid
userDate = DateValue(txtInput.Text) ' May crash</code></pre>
<h3 id="use-isdate-for-pre-validation">Use <code>IsDate</code> for Pre-validation</h3>
<pre><code class="language-vbnet">If IsDate(txtInput.Text) Then
processDate = DateValue(txtInput.Text)
Else
MsgBox "Invalid date"
End If</code></pre>
<h3 id="prefer-dateserial-for-programmatic-dates">Prefer <code>DateSerial</code> for Programmatic Dates</h3>
<pre><code class="language-vbnet">' Good - Fast and unambiguous
dt = DateSerial(2025, 12, 25)
' Less ideal - String parsing overhead
dt = DateValue("12/25/2025")</code></pre>
<h3 id="be-aware-of-locale-issues">Be Aware of Locale Issues</h3>
<pre><code class="language-vbnet">' US locale: MM/DD/YYYY
dt = DateValue("3/5/2025") ' March 5 in US
' UK locale: DD/MM/YYYY
dt = DateValue("3/5/2025") ' May 3 in UK
' Use unambiguous formats when possible
dt = DateValue("2025-03-05") ' ISO format, clearer
dt = DateValue("March 5, 2025") ' Text format, clearer</code></pre>
<h2 id="comparison-with-other-functions">Comparison with Other Functions</h2>
<h3 id="datevalue-vs-dateserial"><code>DateValue</code> vs <code>DateSerial</code></h3>
<pre><code class="language-vbnet">' DateValue - From string representation
dt = DateValue("12/25/2025")
' DateSerial - From numeric components (faster, more reliable)
dt = DateSerial(2025, 12, 25)</code></pre>
<h3 id="datevalue-vs-cdate"><code>DateValue</code> vs <code>CDate</code></h3>
<pre><code class="language-vbnet">' DateValue - Returns date portion only (strips time)
dt = DateValue("12/25/2025 3:30 PM") ' Returns 12/25/2025 00:00:00
' CDate - Preserves time information
dt = CDate("12/25/2025 3:30 PM") ' Returns 12/25/2025 15:30:00</code></pre>
<h3 id="datevalue-vs-isdate"><code>DateValue</code> vs <code>IsDate</code></h3>
<pre><code class="language-vbnet">' IsDate - Tests if string can be converted (returns Boolean)
If IsDate("12/25/2025") Then...
' DateValue - Actually converts (returns Date or error)
dt = DateValue("12/25/2025")</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Locale-dependent interpretation can cause unexpected results</li>
<li>Cannot directly parse custom date formats</li>
<li>Limited control over parsing rules</li>
<li>Strips time information (use <code>CDate</code> to preserve time)</li>
<li>Two-digit year interpretation fixed (0-29=2000-2029, 30-99=1930-1999)</li>
<li>Error handling required for user input</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>CDate</code>: Converts expression to <code>Date</code> (preserves time)</li>
<li><code>DateSerial</code>: Creates date from year, month, day (numeric)</li>
<li><code>IsDate</code>: Tests if expression can be converted to date</li>
<li><code>Format</code>: Formats date as <code>String</code> (opposite direction)</li>
<li><code>TimeValue</code>: Returns time portion from <code>String</code></li>
<li><code>Year</code>, <code>Month</code>, <code>Day</code>: Extract date components</li>
<li><code>Date</code>: Returns current system date</li>
<li><code>CVDate</code>: Converts expression to <code>Date</code> (legacy function)</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>