<!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 - date - Datetime">
<title>date - 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> / date</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="date-function">Date Function</h1>
<p>Returns the current system date as a <code>Variant</code> of subtype <code>Date</code>.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Date</code></pre>
<h2 id="parameters">Parameters</h2>
<p>None. The <code>Date</code> function takes no parameters.</p>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Variant</code> of subtype <code>Date</code> (<code>VarType = 7</code>) containing the current system date.
The time portion is set to midnight (00:00:00).</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Date</code> function returns the current date from the system clock. Unlike <code>Now</code>,
which returns both date and time, <code>Date</code> returns only the date portion with the
time set to midnight.
<strong>Important Characteristics:</strong>
- Returns only the date portion (time is midnight)
- Uses system date from computer's clock
- Can also be used as a statement to set the system date: <code>Date = #1/1/2025#</code>
- Date values are stored internally as Double (days since Dec 30, 1899)
- <code>VarType</code> of result is 7 (vbDate)
- Locale-aware for display formatting
- Date range: January 1, 100 to December 31, 9999</p>
<h2 id="date-storage-format">Date Storage Format</h2>
<p>Internally, dates are stored as Double precision floating-point numbers:
- Integer part: Number of days since December 30, 1899
- Fractional part: Time of day (0.0 = midnight, 0.5 = noon, etc.)
- Date function always returns fractional part as 0.0</p>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">' Get current date
Dim today As Date
today = Date
MsgBox "Today is: " & today
' Display in message box
MsgBox "Current date: " & Date
' Store in Variant
Dim currentDate As Variant
currentDate = Date</code></pre>
<h3 id="date-calculations">Date Calculations</h3>
<pre><code class="language-vbnet">' Calculate days until end of year
Dim daysLeft As Long
daysLeft = DateSerial(Year(Date), 12, 31) - Date
MsgBox daysLeft & " days left in the year"
' Add 30 days to current date
Dim futureDate As Date
futureDate = Date + 30
MsgBox "30 days from now: " & futureDate
' Subtract dates to get difference
Dim startDate As Date
Dim daysPassed As Long
startDate = #1/1/2025#
daysPassed = Date - startDate</code></pre>
<h3 id="date-comparison">Date Comparison</h3>
<pre><code class="language-vbnet">' Check if date is in the past
Dim deadline As Date
deadline = #12/31/2025#
If Date > deadline Then
MsgBox "Deadline has passed!"
Else
MsgBox "Still time remaining"
End If
' Compare with specific date
If Date = #1/1/2025# Then
MsgBox "Happy New Year!"
End If</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="date-stamping-records">Date Stamping Records</h3>
<pre><code class="language-vbnet">' Add timestamp to database record
rs.AddNew
rs("CustomerName") = txtName.Text
rs("OrderDate") = Date
rs("OrderTime") = Time
rs.Update
' File naming with date
Dim fileName As String
fileName = "Report_" & Format(Date, "yyyymmdd") & ".txt"</code></pre>
<h3 id="date-validation">Date Validation</h3>
<pre><code class="language-vbnet">Function IsDateInRange(checkDate As Date, startDate As Date, endDate As Date) As Boolean
IsDateInRange = (checkDate >= startDate And checkDate <= endDate)
End Function
' Check if today is within range
If IsDateInRange(Date, #1/1/2025#, #12/31/2025#) Then
MsgBox "Date is within 2025"
End If</code></pre>
<h3 id="age-calculation">Age Calculation</h3>
<pre><code class="language-vbnet">Function CalculateAge(birthDate As Date) As Integer
Dim age As Integer
age = Year(Date) - Year(birthDate)
' Adjust if birthday hasn't occurred yet this year
If Month(Date) < Month(birthDate) Or _
(Month(Date) = Month(birthDate) And Day(Date) < Day(birthDate)) Then
age = age - 1
End If
CalculateAge = age
End Function</code></pre>
<h3 id="business-days-calculation">Business Days Calculation</h3>
<pre><code class="language-vbnet">Function IsWeekday(checkDate As Date) As Boolean
Dim dayOfWeek As Integer
dayOfWeek = Weekday(checkDate)
IsWeekday = (dayOfWeek > 1 And dayOfWeek < 7) ' Not Sunday(1) or Saturday(7)
End Function
Function AddBusinessDays(startDate As Date, days As Integer) As Date
Dim currentDate As Date
Dim daysAdded As Integer
currentDate = startDate
daysAdded = 0
Do While daysAdded < days
currentDate = currentDate + 1
If IsWeekday(currentDate) Then
daysAdded = daysAdded + 1
End If
Loop
AddBusinessDays = currentDate
End Function</code></pre>
<h3 id="date-range-reporting">Date Range Reporting</h3>
<pre><code class="language-vbnet">Sub GenerateMonthlyReport()
Dim firstDay As Date
Dim lastDay As Date
' Get first day of current month
firstDay = DateSerial(Year(Date), Month(Date), 1)
' Get last day of current month
lastDay = DateSerial(Year(Date), Month(Date) + 1, 0)
' Generate report for date range
MsgBox "Report period: " & firstDay & " to " & lastDay
End Sub</code></pre>
<h3 id="date-based-file-organization">Date-Based File Organization</h3>
<pre><code class="language-vbnet">Function GetArchiveFolder() As String
Dim folderPath As String
folderPath = "C:\Archive\" & Year(Date) & "\" & Format(Date, "mm")
' Create folder if it doesn't exist
If Dir(folderPath, vbDirectory) = "" Then
MkDir folderPath
End If
GetArchiveFolder = folderPath
End Function</code></pre>
<h3 id="expiration-checking">Expiration Checking</h3>
<pre><code class="language-vbnet">Function IsExpired(expirationDate As Date) As Boolean
IsExpired = (Date > expirationDate)
End Function
Function DaysUntilExpiration(expirationDate As Date) As Long
If IsExpired(expirationDate) Then
DaysUntilExpiration = 0
Else
DaysUntilExpiration = expirationDate - Date
End If
End Function</code></pre>
<h3 id="logging-with-timestamps">Logging with Timestamps</h3>
<pre><code class="language-vbnet">Sub LogMessage(message As String)
Dim logFile As Integer
Dim logFileName As String
' Create daily log file
logFileName = "Log_" & Format(Date, "yyyy-mm-dd") & ".txt"
logFile = FreeFile
Open logFileName For Append As logFile
Print #logFile, Date & " " & Time & ": " & message
Close logFile
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="date-cache-for-performance">Date Cache for Performance</h3>
<pre><code class="language-vbnet">' Module-level variables
Private m_cachedDate As Date
Private m_cacheValid As Boolean
Function GetTodaysDate() As Date
' Cache date to avoid repeated system calls in tight loops
Static lastCheck As Date
If Not m_cacheValid Or lastCheck <> Date Then
m_cachedDate = Date
lastCheck = m_cachedDate
m_cacheValid = True
End If
GetTodaysDate = m_cachedDate
End Function</code></pre>
<h3 id="fiscal-year-calculations">Fiscal Year Calculations</h3>
<pre><code class="language-vbnet">Function GetFiscalYear(Optional checkDate As Variant) As Integer
Dim workDate As Date
' Use current date if not specified
If IsMissing(checkDate) Then
workDate = Date
Else
workDate = checkDate
End If
' Fiscal year starts April 1
If Month(workDate) < 4 Then
GetFiscalYear = Year(workDate) - 1
Else
GetFiscalYear = Year(workDate)
End If
End Function
Function GetFiscalQuarter(Optional checkDate As Variant) As Integer
Dim workDate As Date
Dim fiscalMonth As Integer
If IsMissing(checkDate) Then
workDate = Date
Else
workDate = checkDate
End If
' Calculate month in fiscal year (April = 1)
fiscalMonth = Month(workDate) - 3
If fiscalMonth <= 0 Then fiscalMonth = fiscalMonth + 12
' Determine quarter
GetFiscalQuarter = Int((fiscalMonth - 1) / 3) + 1
End Function</code></pre>
<h3 id="date-sequence-generator">Date Sequence Generator</h3>
<pre><code class="language-vbnet">Function GenerateDateRange(startDate As Date, endDate As Date) As Variant
Dim dates() As Date
Dim currentDate As Date
Dim index As Long
Dim dayCount As Long
dayCount = endDate - startDate + 1
ReDim dates(0 To dayCount - 1)
currentDate = startDate
For index = 0 To dayCount - 1
dates(index) = currentDate
currentDate = currentDate + 1
Next index
GenerateDateRange = dates
End Function</code></pre>
<h3 id="date-based-conditional-logic">Date-Based Conditional Logic</h3>
<pre><code class="language-vbnet">Function GetDiscountRate() As Double
Dim dayOfMonth As Integer
dayOfMonth = Day(Date)
Select Case dayOfMonth
Case 1 To 10
GetDiscountRate = 0.1 ' 10% early month discount
Case 11 To 20
GetDiscountRate = 0.05 ' 5% mid-month discount
Case Else
GetDiscountRate = 0 ' No discount
End Select
End Function</code></pre>
<h2 id="date-vs-now-vs-time">Date vs Now vs Time</h2>
<pre><code class="language-vbnet">' Date - Returns only date portion (time = midnight)
Dim d As Date
d = Date ' Example: 1/15/2025 12:00:00 AM
' Now - Returns date and time
Dim n As Date
n = Now ' Example: 1/15/2025 2:30:45 PM
' Time - Returns only time portion (date = Dec 30, 1899)
Dim t As Date
t = Time ' Example: 12/30/1899 2:30:45 PM
' Extract components
Dim today As Date
today = Date ' Gets just the date
Dim dateTime As Date
dateTime = today + Time ' Combines current date and time</code></pre>
<h2 id="setting-the-system-date">Setting the System Date</h2>
<pre><code class="language-vbnet">' Date can also be used as a statement (requires admin rights)
Date = #1/1/2025# ' Sets system date
' More common: just read the date
Dim currentDate As Date
currentDate = Date ' Read-only operation</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Date</code> is a system call that reads the system clock</li>
<li>In tight loops, consider caching if the date won't change during execution</li>
<li>Faster than <code>Now</code> when you only need the date portion</li>
<li>Date comparisons are fast (numeric comparison of Double values)</li>
<li>Format conversions can be slower than raw date operations</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="cache-in-long-running-loops">Cache in Long-Running Loops</h3>
<pre><code class="language-vbnet">' Inefficient - calls Date repeatedly
For i = 1 To 10000
If records(i).ExpiryDate < Date Then
' Process expired record
End If
Next i
' Better - cache the date
Dim today As Date
today = Date
For i = 1 To 10000
If records(i).ExpiryDate < today Then
' Process expired record
End If
Next i</code></pre>
<h3 id="use-appropriate-date-functions">Use Appropriate Date Functions</h3>
<pre><code class="language-vbnet">' Good - Use Date for date-only operations
Dim orderDate As Date
orderDate = Date
' Good - Use Now for timestamps
Dim timestamp As Date
timestamp = Now
' Avoid - Don't extract date from Now if you just need date
Dim today As Date
today = Int(Now) ' Works but less clear than Date</code></pre>
<h3 id="store-dates-as-date-type">Store Dates as Date Type</h3>
<pre><code class="language-vbnet">' Good - Proper type usage
Dim birthDate As Date
birthDate = Date
' Avoid - String storage loses type safety and comparison capability
Dim birthDateStr As String
birthDateStr = CStr(Date) ' Comparison becomes string-based</code></pre>
<h2 id="locale-considerations">Locale Considerations</h2>
<ul>
<li>Display format depends on system locale settings</li>
<li>Internal storage is always the same (Double)</li>
<li>Use <code>Format</code> function for explicit formatting</li>
<li>Date literals use US format (#MM/DD/YYYY#) regardless of locale</li>
</ul>
<pre><code class="language-vbnet">' Display varies by locale
MsgBox Date ' US: 1/15/2025, UK: 15/01/2025
' Explicit formatting
MsgBox Format(Date, "yyyy-mm-dd") ' Always: 2025-01-15</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot directly extract error information from invalid dates</li>
<li>System date setting requires administrative privileges</li>
<li>Date range limited to January 1, 100 through December 31, 9999</li>
<li>Accuracy depends on system clock</li>
<li>No built-in timezone support</li>
<li>Daylight saving time handled by system, not VB6</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Now</code>: Returns current date and time</li>
<li><code>Time</code>: Returns current time (date portion is Dec 30, 1899)</li>
<li><code>DateSerial</code>: Creates a date from year, month, and day values</li>
<li><code>DateValue</code>: Converts a string to a date</li>
<li><code>Year</code>, <code>Month</code>, <code>Day</code>: Extract components from a date</li>
<li><code>Weekday</code>: Returns day of week (1=Sunday, 7=Saturday)</li>
<li><code>DateAdd</code>: Adds a time interval to a date</li>
<li><code>DateDiff</code>: Returns the difference between two dates</li>
<li><code>DatePart</code>: Returns a specific part of a date</li>
<li><code>Format</code>: Formats a date as a string</li>
<li><code>IsDate</code>: Tests if a value can be converted to a date</li>
<li><code>CDate</code>: Converts an expression to a 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 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>