<!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 - day - Datetime">
<title>day - 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> / day</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="day-function">Day Function</h1>
<p>Returns a whole number between 1 and 31, inclusive, representing the day of the month.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Day(date)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>date</strong>: Required. Any Variant, numeric expression, string expression, or any combination
that can represent a date. If date contains Null, Null is returned.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns an Integer representing the day of the month (1-31). Returns Null if the date
parameter contains Null.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Day</code> function extracts the day component from a date value. It's one of the primary
date component extraction functions in VB6, along with <code>Year</code>, <code>Month</code>, <code>Weekday</code>, <code>Hour</code>,
<code>Minute</code>, and <code>Second</code>.
<strong>Important Characteristics:</strong>
- Returns values 1-31 depending on the month
- Works with Date variables, date literals, and date expressions
- Returns Null if input is Null (Variant behavior)
- Automatically handles different month lengths
- Time portion of datetime values is ignored
- Type mismatch error if argument cannot be interpreted as date
- Can be used with date arithmetic results</p>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">' Extract day from date literal
Dim d As Integer
d = Day(#1/15/2025#) ' Returns 15
' From Date variable
Dim birthday As Date
birthday = #5/23/1990#
d = Day(birthday) ' Returns 23
' From current date
d = Day(Date) ' Returns current day</code></pre>
<h3 id="with-date-functions">With Date Functions</h3>
<pre><code class="language-vbnet">' Extract day from DateSerial result
Dim constructedDate As Date
constructedDate = DateSerial(2025, 12, 25)
Dim dayNum As Integer
dayNum = Day(constructedDate) ' Returns 25
' From DateAdd calculation
Dim futureDate As Date
futureDate = DateAdd("d", 10, Date)
dayNum = Day(futureDate)</code></pre>
<h3 id="date-validation">Date Validation</h3>
<pre><code class="language-vbnet">Function IsLastDayOfMonth(dateValue As Date) As Boolean
Dim nextDay As Date
nextDay = DateAdd("d", 1, dateValue)
IsLastDayOfMonth = (Day(nextDay) = 1)
End Function
' Alternative using month comparison
Function IsLastDayOfMonth2(dateValue As Date) As Boolean
IsLastDayOfMonth2 = (Day(dateValue + 1) = 1)
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="get-days-in-month">Get Days in Month</h3>
<pre><code class="language-vbnet">Function DaysInMonth(dateValue As Date) As Integer
' Get last day of the month
Dim firstOfNextMonth As Date
firstOfNextMonth = DateSerial(Year(dateValue), Month(dateValue) + 1, 1)
DaysInMonth = Day(firstOfNextMonth - 1)
End Function
' Alternative approach
Function DaysInMonth2(yr As Integer, mo As Integer) As Integer
DaysInMonth2 = Day(DateSerial(yr, mo + 1, 0))
End Function</code></pre>
<h3 id="extract-date-components">Extract Date Components</h3>
<pre><code class="language-vbnet">Sub DisplayDateParts(dateValue As Date)
Dim yr As Integer, mo As Integer, dy As Integer
yr = Year(dateValue)
mo = Month(dateValue)
dy = Day(dateValue)
MsgBox "Year: " & yr & ", Month: " & mo & ", Day: " & dy
End Sub</code></pre>
<h3 id="day-based-filtering">Day-Based Filtering</h3>
<pre><code class="language-vbnet">Function IsFirstHalfOfMonth(dateValue As Date) As Boolean
IsFirstHalfOfMonth = (Day(dateValue) <= 15)
End Function
Function IsSecondHalfOfMonth(dateValue As Date) As Boolean
IsSecondHalfOfMonth = (Day(dateValue) > 15)
End Function
Function IsMonthStart(dateValue As Date) As Boolean
IsMonthStart = (Day(dateValue) = 1)
End Function</code></pre>
<h3 id="date-comparison-by-day">Date Comparison by Day</h3>
<pre><code class="language-vbnet">Function SameDayOfMonth(date1 As Date, date2 As Date) As Boolean
SameDayOfMonth = (Day(date1) = Day(date2))
End Function
Function CompareDays(date1 As Date, date2 As Date) As Integer
' Returns: -1 if date1's day < date2's day
' 0 if same day
' 1 if date1's day > date2's day
Dim d1 As Integer, d2 As Integer
d1 = Day(date1)
d2 = Day(date2)
If d1 < d2 Then
CompareDays = -1
ElseIf d1 > d2 Then
CompareDays = 1
Else
CompareDays = 0
End If
End Function</code></pre>
<h3 id="reconstruct-date-with-modified-day">Reconstruct Date with Modified Day</h3>
<pre><code class="language-vbnet">Function ChangeDay(originalDate As Date, newDay As Integer) As Date
' Create new date with same year/month but different day
ChangeDay = DateSerial(Year(originalDate), Month(originalDate), newDay)
End Function
' Move to specific day of current month
Function MoveToDayOfMonth(dayNum As Integer) As Date
MoveToDayOfMonth = DateSerial(Year(Date), Month(Date), dayNum)
End Function</code></pre>
<h3 id="loop-through-days-of-month">Loop Through Days of Month</h3>
<pre><code class="language-vbnet">Sub ProcessAllDaysInMonth(yr As Integer, mo As Integer)
Dim dayCount As Integer
Dim currentDay As Date
Dim i As Integer
dayCount = Day(DateSerial(yr, mo + 1, 0))
For i = 1 To dayCount
currentDay = DateSerial(yr, mo, i)
Debug.Print "Day " & Day(currentDay) & ": " & Format(currentDay, "dddd")
Next i
End Sub</code></pre>
<h3 id="pay-period-calculations">Pay Period Calculations</h3>
<pre><code class="language-vbnet">Function GetPayPeriod(dateValue As Date) As String
' Bi-monthly pay periods: 1-15 and 16-end
If Day(dateValue) <= 15 Then
GetPayPeriod = "First Half"
Else
GetPayPeriod = "Second Half"
End If
End Function
Function IsPayday(dateValue As Date) As Boolean
Dim dy As Integer
dy = Day(dateValue)
' Payday on 15th and last day of month
IsPayday = (dy = 15) Or (Day(dateValue + 1) = 1)
End Function</code></pre>
<h3 id="birthdayanniversary-checks">Birthday/Anniversary Checks</h3>
<pre><code class="language-vbnet">Function IsBirthdayMonth(birthday As Date) As Boolean
' Check if current month/day matches birthday
IsBirthdayMonth = (Month(Date) = Month(birthday)) And _
(Day(Date) = Day(birthday))
End Function
Function DaysUntilBirthday(birthday As Date) As Integer
Dim thisYearBirthday As Date
thisYearBirthday = DateSerial(Year(Date), Month(birthday), Day(birthday))
If thisYearBirthday < Date Then
' Birthday already passed this year
thisYearBirthday = DateSerial(Year(Date) + 1, Month(birthday), Day(birthday))
End If
DaysUntilBirthday = DateDiff("d", Date, thisYearBirthday)
End Function</code></pre>
<h3 id="data-grouping-by-day">Data Grouping by Day</h3>
<pre><code class="language-vbnet">Function GetDayGroup(dateValue As Date) As String
Dim dy As Integer
dy = Day(dateValue)
Select Case dy
Case 1 To 10
GetDayGroup = "Days 1-10"
Case 11 To 20
GetDayGroup = "Days 11-20"
Case Else
GetDayGroup = "Days 21-31"
End Select
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="calendar-grid-generator">Calendar Grid Generator</h3>
<pre><code class="language-vbnet">Sub GenerateMonthCalendar(yr As Integer, mo As Integer)
Dim firstDay As Date
Dim lastDay As Date
Dim currentDate As Date
Dim dayOfWeek As Integer
Dim daysInMonth As Integer
firstDay = DateSerial(yr, mo, 1)
daysInMonth = Day(DateSerial(yr, mo + 1, 0))
' Print header
Debug.Print "Sun Mon Tue Wed Thu Fri Sat"
' Print leading spaces
dayOfWeek = Weekday(firstDay, vbSunday)
Debug.Print String((dayOfWeek - 1) * 4, " ");
' Print days
For i = 1 To daysInMonth
currentDate = DateSerial(yr, mo, i)
Debug.Print Format(Day(currentDate), "000") & " ";
If Weekday(currentDate, vbSunday) = 7 Then
Debug.Print ' New line
End If
Next i
End Sub</code></pre>
<h3 id="date-range-iterator">Date Range Iterator</h3>
<pre><code class="language-vbnet">Function GetDateRange(startDate As Date, endDate As Date) As Variant
' Returns array of dates in range
Dim dayCount As Long
Dim dates() As Date
Dim i As Long
dayCount = DateDiff("d", startDate, endDate) + 1
ReDim dates(0 To dayCount - 1)
For i = 0 To dayCount - 1
dates(i) = DateAdd("d", i, startDate)
Debug.Print "Day " & Day(dates(i)) & ": " & dates(i)
Next i
GetDateRange = dates
End Function</code></pre>
<h3 id="billing-cycle-calculator">Billing Cycle Calculator</h3>
<pre><code class="language-vbnet">Function CalculateBillingDay(startDate As Date, monthsElapsed As Integer) As Date
' Keep same day-of-month for billing cycle
Dim billingDay As Integer
billingDay = Day(startDate)
CalculateBillingDay = DateSerial( _
Year(DateAdd("m", monthsElapsed, startDate)), _
Month(DateAdd("m", monthsElapsed, startDate)), _
billingDay)
End Function
Function AdjustForMonthEnd(targetDate As Date, originalDay As Integer) As Date
' Handle when original day doesn't exist in target month
Dim targetMonth As Date
Dim maxDayInMonth As Integer
targetMonth = DateSerial(Year(targetDate), Month(targetDate), 1)
maxDayInMonth = Day(DateSerial(Year(targetDate), Month(targetDate) + 1, 0))
If originalDay > maxDayInMonth Then
AdjustForMonthEnd = DateSerial(Year(targetDate), Month(targetDate), maxDayInMonth)
Else
AdjustForMonthEnd = DateSerial(Year(targetDate), Month(targetDate), originalDay)
End If
End Function</code></pre>
<h3 id="date-pattern-analyzer">Date Pattern Analyzer</h3>
<pre><code class="language-vbnet">Function AnalyzeDatePattern(dates() As Date) As String
' Determine if dates follow a pattern
Dim i As Long
Dim allSameDay As Boolean
Dim firstDay As Integer
If UBound(dates) < LBound(dates) Then
AnalyzeDatePattern = "Empty"
Exit Function
End If
firstDay = Day(dates(LBound(dates)))
allSameDay = True
For i = LBound(dates) + 1 To UBound(dates)
If Day(dates(i)) <> firstDay Then
allSameDay = False
Exit For
End If
Next i
If allSameDay Then
AnalyzeDatePattern = "Monthly on day " & firstDay
Else
AnalyzeDatePattern = "Variable days"
End If
End Function</code></pre>
<h3 id="work-schedule-helper">Work Schedule Helper</h3>
<pre><code class="language-vbnet">Function IsWorkDay(dateValue As Date, workDays As String) As Boolean
' workDays: comma-separated list like "1,15,30"
Dim dayList() As String
Dim i As Integer
Dim currentDay As Integer
currentDay = Day(dateValue)
dayList = Split(workDays, ",")
For i = LBound(dayList) To UBound(dayList)
If CInt(Trim(dayList(i))) = currentDay Then
IsWorkDay = True
Exit Function
End If
Next i
IsWorkDay = False
End Function</code></pre>
<h3 id="leap-year-day-check">Leap Year Day Check</h3>
<pre><code class="language-vbnet">Function IsLeapYearDay(dateValue As Date) As Boolean
' Check if date is February 29
IsLeapYearDay = (Month(dateValue) = 2) And (Day(dateValue) = 29)
End Function
Function HasLeapYearDayBetween(startDate As Date, endDate As Date) As Boolean
Dim yr As Integer
Dim leapDay As Date
For yr = Year(startDate) To Year(endDate)
On Error Resume Next
leapDay = DateSerial(yr, 2, 29)
If Err.Number = 0 Then
If leapDay >= startDate And leapDay <= endDate Then
HasLeapYearDayBetween = True
Exit Function
End If
End If
Err.Clear
Next yr
HasLeapYearDayBetween = False
End Function</code></pre>
<h3 id="database-query-builder">Database Query Builder</h3>
<pre><code class="language-vbnet">Function BuildDayFilter(targetDay As Integer) As String
' Build SQL WHERE clause for specific day of month
BuildDayFilter = "Day(DateField) = " & targetDay
End Function
Function GetRecordsForDayRange(startDay As Integer, endDay As Integer) As String
GetRecordsForDayRange = "Day(DateField) BETWEEN " & startDay & " AND " & endDay
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeDay(dateValue As Variant) As Variant
On Error GoTo ErrorHandler
If IsNull(dateValue) Then
SafeDay = Null
Exit Function
End If
If Not IsDate(dateValue) Then
SafeDay = Null
Exit Function
End If
SafeDay = Day(dateValue)
Exit Function
ErrorHandler:
SafeDay = Null
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 13</strong> (Type mismatch): Argument is not a valid date</li>
<li><strong>Error 94</strong> (Invalid use of Null): Using Null date without handling</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Day</code> is a fast, direct component extraction</li>
<li>More efficient than string parsing with <code>Format</code></li>
<li>No performance difference between extracting from Date vs Variant</li>
<li>Cache results when using repeatedly on same date</li>
<li>Combine with <code>Year</code> and <code>Month</code> for full date decomposition</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="use-for-component-extraction">Use for Component Extraction</h3>
<pre><code class="language-vbnet">' Good - Direct component access
dayNum = Day(someDate)
' Avoid - String manipulation overhead
dayNum = CInt(Format(someDate, "dd"))</code></pre>
<h3 id="validate-date-before-extraction">Validate Date Before Extraction</h3>
<pre><code class="language-vbnet">' Good - Check for valid date
If IsDate(userInput) Then
dayNum = Day(CDate(userInput))
End If
' Avoid - May cause type mismatch error
dayNum = Day(userInput)</code></pre>
<h3 id="handle-null-values-in-variants">Handle Null Values in Variants</h3>
<pre><code class="language-vbnet">' Good - Check for Null
If Not IsNull(dateVariant) Then
dayNum = Day(dateVariant)
End If
' Avoid - May propagate Null unexpectedly
dayNum = Day(dateVariant)</code></pre>
<h3 id="combine-with-dateserial-for-date-manipulation">Combine with <code>DateSerial</code> for Date Manipulation</h3>
<pre><code class="language-vbnet">' Good - Reconstruct date with modified component
newDate = DateSerial(Year(oldDate), Month(oldDate), 15)
' Good - Get last day of month
lastDay = Day(DateSerial(yr, mo + 1, 0))</code></pre>
<h2 id="comparison-with-other-functions">Comparison with Other Functions</h2>
<h3 id="day-vs-format"><code>Day</code> vs <code>Format</code></h3>
<pre><code class="language-vbnet">' Day - Returns integer, fast
dayNum = Day(someDate) ' Returns 15
' Format - Returns string, slower, more flexible
dayStr = Format(someDate, "dd") ' Returns "15"
dayStr = Format(someDate, "d") ' Returns "15" (no leading zero)</code></pre>
<h3 id="day-vs-datepart"><code>Day</code> vs <code>DatePart</code></h3>
<pre><code class="language-vbnet">' Day - Specific function for day of month
dayNum = Day(someDate)
' DatePart - Generic function, can extract any part
dayNum = DatePart("d", someDate) ' Same result</code></pre>
<h3 id="day-vs-weekday"><code>Day</code> vs <code>Weekday</code></h3>
<pre><code class="language-vbnet">' Day - Day of month (1-31)
dayOfMonth = Day(#1/15/2025#) ' Returns 15
' Weekday - Day of week (1-7)
dayOfWeek = Weekday(#1/15/2025#) ' Returns 4 (Wednesday)</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Only returns day of month, not day of year or week</li>
<li>Returns <code>Null</code> for <code>Null</code> input (<code>Variant</code> propagation)</li>
<li>No built-in validation of day validity for given month</li>
<li>Does not indicate if day is weekend, holiday, etc.</li>
<li>Cannot distinguish between different months with same day</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Year</code>: Extracts year component from date</li>
<li><code>Month</code>: Extracts month component from date</li>
<li><code>Weekday</code>: Returns day of week (1-7)</li>
<li><code>DatePart</code>: Generic date part extraction</li>
<li><code>DateSerial</code>: Constructs date from components</li>
<li><code>Format</code>: Formats date as string with custom patterns</li>
<li><code>Hour</code>, <code>Minute</code>, <code>Second</code>: Extract time components</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>