<!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 - dateadd - Datetime">
<title>dateadd - 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> / dateadd</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="dateadd-function">DateAdd Function</h1>
<p>Returns a <code>Variant</code> (<code>Date</code>) containing a date to which a specified time interval has been added.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">DateAdd(interval, number, date)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong><code>interval</code></strong>: Required. <code>String</code> expression that is the interval of time you want to add.
See the Interval Settings section for valid values.</li>
<li><strong><code>number</code></strong>: Required. <code>Numeric</code> expression that is the number of intervals you want to add.
Can be positive (to get dates in the future) or negative (to get dates in the past).</li>
<li><strong><code>date</code></strong>: Required. <code>Variant</code> (<code>Date</code>) or literal representing the date to which the interval is added.</li>
</ul>
<h2 id="interval-settings">Interval Settings</h2>
<p>The <code>interval</code> parameter can have the following values:</p>
<table>
<thead>
<tr>
<th>Setting</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>"yyyy"</td>
<td>Year</td>
</tr>
<tr>
<td>"q"</td>
<td>Quarter</td>
</tr>
<tr>
<td>"m"</td>
<td>Month</td>
</tr>
<tr>
<td>"y"</td>
<td>Day of year</td>
</tr>
<tr>
<td>"d"</td>
<td>Day</td>
</tr>
<tr>
<td>"w"</td>
<td>Weekday</td>
</tr>
<tr>
<td>"ww"</td>
<td>Week of year</td>
</tr>
<tr>
<td>"h"</td>
<td>Hour</td>
</tr>
<tr>
<td>"n"</td>
<td>Minute</td>
</tr>
<tr>
<td>"s"</td>
<td>Second</td>
</tr>
</tbody>
</table>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Variant</code> of subtype <code>Date</code> containing the result of adding the specified interval
to the given date. Returns Null if any parameter is Null.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>DateAdd</code> function is used to add or subtract a specified time interval from a date.
You can use it to calculate future or past dates relative to a known date.
<strong>Important Characteristics:</strong>
- Negative numbers subtract intervals (dates in the past)
- Positive numbers add intervals (dates in the future)
- Handles month-end dates intelligently (e.g., adding 1 month to Jan 31 gives Feb 28/29)
- When adding months, if the resulting day doesn't exist, uses last day of month
- Respects daylight saving time transitions
- Week ("ww") interval treats Sunday as the first day of the week
- Weekday ("w") interval is equivalent to day ("d") interval
- Day of year ("y") interval is equivalent to day ("d") interval</p>
<h2 id="month-and-year-calculations">Month and Year Calculations</h2>
<p>When adding months or years, <code>DateAdd</code> ensures the result is valid:
- Jan 31 + 1 month = Feb 28 (or 29 in leap year)
- Jan 31 + 2 months = Mar 31
- Aug 31 - 3 months = May 31</p>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">' Add days to a date
Dim futureDate As Date
futureDate = DateAdd("d", 30, Date)
MsgBox "30 days from now: " & futureDate
' Subtract days from a date
Dim pastDate As Date
pastDate = DateAdd("d", -7, Date)
MsgBox "A week ago: " & pastDate
' Add months
Dim nextMonth As Date
nextMonth = DateAdd("m", 1, Date)
MsgBox "One month from now: " & nextMonth</code></pre>
<h3 id="different-time-intervals">Different Time Intervals</h3>
<pre><code class="language-vbnet">Dim startDate As Date
startDate = #1/15/2025#
' Add years
MsgBox "Next year: " & DateAdd("yyyy", 1, startDate)
' Add quarters
MsgBox "Next quarter: " & DateAdd("q", 1, startDate)
' Add weeks
MsgBox "Next week: " & DateAdd("ww", 1, startDate)
' Add hours
MsgBox "In 6 hours: " & DateAdd("h", 6, startDate)
' Add minutes
MsgBox "In 90 minutes: " & DateAdd("n", 90, startDate)
' Add seconds
MsgBox "In 3600 seconds: " & DateAdd("s", 3600, startDate)</code></pre>
<h3 id="working-with-past-dates">Working with Past Dates</h3>
<pre><code class="language-vbnet">' Calculate date 90 days ago
Dim quarterAgo As Date
quarterAgo = DateAdd("d", -90, Date)
' Calculate date 1 year ago
Dim yearAgo As Date
yearAgo = DateAdd("yyyy", -1, Date)
' Calculate date 3 months ago
Dim threeMonthsAgo As Date
threeMonthsAgo = DateAdd("m", -3, Date)</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="due-date-calculation">Due Date Calculation</h3>
<pre><code class="language-vbnet">Function CalculateDueDate(invoiceDate As Date, terms As Integer) As Date
' NET 30, NET 60, etc.
CalculateDueDate = DateAdd("d", terms, invoiceDate)
End Function
' Usage
Dim invoice As Date
Dim dueDate As Date
invoice = Date
dueDate = CalculateDueDate(invoice, 30) ' Due in 30 days</code></pre>
<h3 id="age-based-eligibility">Age-Based Eligibility</h3>
<pre><code class="language-vbnet">Function IsOldEnough(birthDate As Date, requiredAge As Integer) As Boolean
Dim eligibilityDate As Date
eligibilityDate = DateAdd("yyyy", requiredAge, birthDate)
IsOldEnough = (Date >= eligibilityDate)
End Function
' Usage
If IsOldEnough(#5/10/2005#, 18) Then
MsgBox "Eligible"
End If</code></pre>
<h3 id="expiration-date-setting">Expiration Date Setting</h3>
<pre><code class="language-vbnet">Function SetExpirationDate(startDate As Date, months As Integer) As Date
SetExpirationDate = DateAdd("m", months, startDate)
End Function
' Set license to expire in 12 months
Dim license As Date
license = Date
Dim expires As Date
expires = SetExpirationDate(license, 12)</code></pre>
<h3 id="meeting-schedule">Meeting Schedule</h3>
<pre><code class="language-vbnet">Function GetNextMeeting(lastMeeting As Date, interval As String, count As Integer) As Date
GetNextMeeting = DateAdd(interval, count, lastMeeting)
End Function
' Weekly meeting
Dim nextWeekly As Date
nextWeekly = GetNextMeeting(#1/15/2025#, "ww", 1)
' Monthly meeting
Dim nextMonthly As Date
nextMonthly = GetNextMeeting(#1/15/2025#, "m", 1)</code></pre>
<h3 id="subscription-renewal">Subscription Renewal</h3>
<pre><code class="language-vbnet">Sub CalculateRenewalDates()
Dim startDate As Date
Dim firstRenewal As Date
Dim secondRenewal As Date
startDate = Date
firstRenewal = DateAdd("m", 12, startDate) ' Annual renewal
secondRenewal = DateAdd("m", 24, startDate) ' Second year
MsgBox "Start: " & startDate & vbCrLf & _
"First renewal: " & firstRenewal & vbCrLf & _
"Second renewal: " & secondRenewal
End Sub</code></pre>
<h3 id="trial-period-end">Trial Period End</h3>
<pre><code class="language-vbnet">Function GetTrialEndDate(startDate As Date, trialDays As Integer) As Date
GetTrialEndDate = DateAdd("d", trialDays, startDate)
End Function
' 30-day trial
Dim trialStart As Date
Dim trialEnd As Date
trialStart = Date
trialEnd = GetTrialEndDate(trialStart, 30)</code></pre>
<h3 id="report-period-calculation">Report Period Calculation</h3>
<pre><code class="language-vbnet">Function GetReportingPeriod(endDate As Date, months As Integer) As Date
' Calculate start date by going back specified months
GetReportingPeriod = DateAdd("m", -months, endDate)
End Function
' Get start of 6-month period ending today
Dim periodStart As Date
periodStart = GetReportingPeriod(Date, 6)</code></pre>
<h3 id="reminder-dates">Reminder Dates</h3>
<pre><code class="language-vbnet">Sub SetReminders(eventDate As Date)
Dim oneWeekBefore As Date
Dim oneDayBefore As Date
Dim oneHourBefore As Date
oneWeekBefore = DateAdd("d", -7, eventDate)
oneDayBefore = DateAdd("d", -1, eventDate)
oneHourBefore = DateAdd("h", -1, eventDate)
' Schedule reminders...
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="business-days-calculation">Business Days Calculation</h3>
<pre><code class="language-vbnet">Function AddBusinessDays(startDate As Date, days As Integer) As Date
Dim result As Date
Dim daysAdded As Integer
Dim direction As Integer
result = startDate
direction = Sgn(days)
daysAdded = 0
Do While Abs(daysAdded) < Abs(days)
result = DateAdd("d", direction, result)
' Skip weekends
If Weekday(result) <> vbSaturday And Weekday(result) <> vbSunday Then
daysAdded = daysAdded + direction
End If
Loop
AddBusinessDays = result
End Function</code></pre>
<h3 id="date-range-generator">Date Range Generator</h3>
<pre><code class="language-vbnet">Function GenerateDateSeries(startDate As Date, interval As String, _
count As Integer, step As Integer) As Variant
Dim dates() As Date
Dim i As Integer
ReDim dates(0 To count - 1)
For i = 0 To count - 1
dates(i) = DateAdd(interval, i * step, startDate)
Next i
GenerateDateSeries = dates
End Function
' Generate 12 month-end dates
Dim monthEnds As Variant
monthEnds = GenerateDateSeries(#1/31/2025#, "m", 12, 1)</code></pre>
<h3 id="fiscal-period-calculator">Fiscal Period Calculator</h3>
<pre><code class="language-vbnet">Function GetFiscalQuarterEnd(fiscalYearStart As Date, quarter As Integer) As Date
Dim quarterStart As Date
Dim quarterEnd As Date
' Calculate start of quarter
quarterStart = DateAdd("m", (quarter - 1) * 3, fiscalYearStart)
' End is 3 months later minus 1 day
quarterEnd = DateAdd("d", -1, DateAdd("m", 3, quarterStart))
GetFiscalQuarterEnd = quarterEnd
End Function</code></pre>
<h3 id="recurring-event-calculator">Recurring Event Calculator</h3>
<pre><code class="language-vbnet">Function GetNextOccurrence(lastOccurrence As Date, frequency As String) As Date
Select Case LCase(frequency)
Case "daily"
GetNextOccurrence = DateAdd("d", 1, lastOccurrence)
Case "weekly"
GetNextOccurrence = DateAdd("ww", 1, lastOccurrence)
Case "biweekly"
GetNextOccurrence = DateAdd("ww", 2, lastOccurrence)
Case "monthly"
GetNextOccurrence = DateAdd("m", 1, lastOccurrence)
Case "quarterly"
GetNextOccurrence = DateAdd("q", 1, lastOccurrence)
Case "annually"
GetNextOccurrence = DateAdd("yyyy", 1, lastOccurrence)
Case Else
GetNextOccurrence = lastOccurrence
End Select
End Function</code></pre>
<h3 id="time-zone-offset-simple">Time Zone Offset (Simple)</h3>
<pre><code class="language-vbnet">Function ConvertToTimeZone(localTime As Date, hourOffset As Integer) As Date
' Simple timezone conversion (doesn't account for DST)
ConvertToTimeZone = DateAdd("h", hourOffset, localTime)
End Function
' Convert EST to PST (3 hours earlier)
Dim estTime As Date
Dim pstTime As Date
estTime = Now
pstTime = ConvertToTimeZone(estTime, -3)</code></pre>
<h3 id="age-calculator-with-precision">Age Calculator with Precision</h3>
<pre><code class="language-vbnet">Function GetExactAge(birthDate As Date) As String
Dim years As Integer
Dim months As Integer
Dim days As Integer
Dim tempDate As Date
' Calculate years
tempDate = birthDate
years = 0
Do While DateAdd("yyyy", years + 1, tempDate) <= Date
years = years + 1
Loop
' Calculate remaining months
tempDate = DateAdd("yyyy", years, birthDate)
months = 0
Do While DateAdd("m", months + 1, tempDate) <= Date
months = months + 1
Loop
' Calculate remaining days
tempDate = DateAdd("m", months, DateAdd("yyyy", years, birthDate))
days = DateDiff("d", tempDate, Date)
GetExactAge = years & " years, " & months & " months, " & days & " days"
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeDateAdd(interval As String, number As Long, _
dateValue As Date) As Variant
On Error GoTo ErrorHandler
' Validate interval
Select Case LCase(interval)
Case "yyyy", "q", "m", "y", "d", "w", "ww", "h", "n", "s"
SafeDateAdd = DateAdd(interval, number, dateValue)
Case Else
SafeDateAdd = Null ' Invalid interval
End Select
Exit Function
ErrorHandler:
SafeDateAdd = Null ' Return Null on error
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 5</strong> (Invalid procedure call): Invalid interval string</li>
<li><strong>Error 13</strong> (Type mismatch): Non-numeric number or non-date date parameter</li>
<li><strong>Error 6</strong> (Overflow): Result date is outside valid range (100-9999 AD)</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>DateAdd</code> is efficient for single date calculations</li>
<li>For large date ranges, consider pre-calculating frequently used values</li>
<li>Month and year additions are slightly slower than day additions</li>
<li>Interval string comparison is case-insensitive but exact strings are faster</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="use-named-constants-for-intervals">Use Named Constants for Intervals</h3>
<pre><code class="language-vbnet">' Define constants for clarity
Const INTERVAL_YEAR As String = "yyyy"
Const INTERVAL_MONTH As String = "m"
Const INTERVAL_DAY As String = "d"
Const INTERVAL_HOUR As String = "h"
' Use in code
nextYear = DateAdd(INTERVAL_YEAR, 1, Date)</code></pre>
<h3 id="validate-input-dates">Validate Input Dates</h3>
<pre><code class="language-vbnet">Function AddDaysToDate(startDate As Variant, days As Integer) As Date
If Not IsDate(startDate) Then
Err.Raise 13, , "Invalid date"
End If
AddDaysToDate = DateAdd("d", days, CDate(startDate))
End Function</code></pre>
<h3 id="handle-month-end-edge-cases">Handle Month-End Edge Cases</h3>
<pre><code class="language-vbnet">' Be aware of month-end behavior
Dim jan31 As Date
jan31 = #1/31/2025#
' Adding 1 month gives Feb 28 (or 29)
Dim result As Date
result = DateAdd("m", 1, jan31) ' Feb 28, 2025
' Adding 2 months gives Mar 31
result = DateAdd("m", 2, jan31) ' Mar 31, 2025</code></pre>
<h2 id="comparison-with-other-date-functions">Comparison with Other Date Functions</h2>
<h3 id="dateadd-vs-datediff"><code>DateAdd</code> vs <code>DateDiff</code></h3>
<pre><code class="language-vbnet">' DateAdd - Adds interval to date, returns new date
Dim future As Date
future = DateAdd("d", 30, Date)
' DateDiff - Calculates interval between dates, returns number
Dim difference As Long
difference = DateDiff("d", Date, future) ' Returns 30</code></pre>
<h3 id="dateadd-vs-simple-arithmetic"><code>DateAdd</code> vs Simple Arithmetic</h3>
<pre><code class="language-vbnet">' Simple arithmetic works for days
Dim tomorrow As Date
tomorrow = Date + 1 ' Same as DateAdd("d", 1, Date)
' But DateAdd is better for months/years
Dim nextMonth As Date
nextMonth = DateAdd("m", 1, Date) ' Handles month-end correctly</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Date range limited to January 1, 100 through December 31, 9999</li>
<li>No built-in support for business day calculations</li>
<li>Doesn't handle holidays automatically</li>
<li>Week starts on Sunday (cannot be customized)</li>
<li>No built-in timezone support</li>
<li>Daylight saving time handled by system, results may vary</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>DateDiff</code>: Returns the number of intervals between two dates</li>
<li><code>DatePart</code>: Returns a specified part of a date</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 date components</li>
<li><code>Hour</code>, <code>Minute</code>, <code>Second</code>: Extract time components</li>
<li><code>Now</code>: Returns current date and time</li>
<li><code>Date</code>: Returns current date</li>
<li><code>Time</code>: Returns current 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>