vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!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">&#x27; Add days to a date
Dim futureDate As Date
futureDate = DateAdd(&quot;d&quot;, 30, Date)
MsgBox &quot;30 days from now: &quot; &amp; futureDate
&#x27; Subtract days from a date
Dim pastDate As Date
pastDate = DateAdd(&quot;d&quot;, -7, Date)
MsgBox &quot;A week ago: &quot; &amp; pastDate
&#x27; Add months
Dim nextMonth As Date
nextMonth = DateAdd(&quot;m&quot;, 1, Date)
MsgBox &quot;One month from now: &quot; &amp; 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#
&#x27; Add years
MsgBox &quot;Next year: &quot; &amp; DateAdd(&quot;yyyy&quot;, 1, startDate)
&#x27; Add quarters
MsgBox &quot;Next quarter: &quot; &amp; DateAdd(&quot;q&quot;, 1, startDate)
&#x27; Add weeks
MsgBox &quot;Next week: &quot; &amp; DateAdd(&quot;ww&quot;, 1, startDate)
&#x27; Add hours
MsgBox &quot;In 6 hours: &quot; &amp; DateAdd(&quot;h&quot;, 6, startDate)
&#x27; Add minutes
MsgBox &quot;In 90 minutes: &quot; &amp; DateAdd(&quot;n&quot;, 90, startDate)
&#x27; Add seconds
MsgBox &quot;In 3600 seconds: &quot; &amp; DateAdd(&quot;s&quot;, 3600, startDate)</code></pre>
<h3 id="working-with-past-dates">Working with Past Dates</h3>
<pre><code class="language-vbnet">&#x27; Calculate date 90 days ago
Dim quarterAgo As Date
quarterAgo = DateAdd(&quot;d&quot;, -90, Date)
&#x27; Calculate date 1 year ago
Dim yearAgo As Date
yearAgo = DateAdd(&quot;yyyy&quot;, -1, Date)
&#x27; Calculate date 3 months ago
Dim threeMonthsAgo As Date
threeMonthsAgo = DateAdd(&quot;m&quot;, -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
    &#x27; NET 30, NET 60, etc.
    CalculateDueDate = DateAdd(&quot;d&quot;, terms, invoiceDate)
End Function
&#x27; Usage
Dim invoice As Date
Dim dueDate As Date
invoice = Date
dueDate = CalculateDueDate(invoice, 30)  &#x27; 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(&quot;yyyy&quot;, requiredAge, birthDate)
    IsOldEnough = (Date &gt;= eligibilityDate)
End Function
&#x27; Usage
If IsOldEnough(#5/10/2005#, 18) Then
    MsgBox &quot;Eligible&quot;
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(&quot;m&quot;, months, startDate)
End Function
&#x27; 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
&#x27; Weekly meeting
Dim nextWeekly As Date
nextWeekly = GetNextMeeting(#1/15/2025#, &quot;ww&quot;, 1)
&#x27; Monthly meeting
Dim nextMonthly As Date
nextMonthly = GetNextMeeting(#1/15/2025#, &quot;m&quot;, 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(&quot;m&quot;, 12, startDate)   &#x27; Annual renewal
    secondRenewal = DateAdd(&quot;m&quot;, 24, startDate)  &#x27; Second year
    MsgBox &quot;Start: &quot; &amp; startDate &amp; vbCrLf &amp; _
           &quot;First renewal: &quot; &amp; firstRenewal &amp; vbCrLf &amp; _
           &quot;Second renewal: &quot; &amp; 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(&quot;d&quot;, trialDays, startDate)
End Function
&#x27; 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
    &#x27; Calculate start date by going back specified months
    GetReportingPeriod = DateAdd(&quot;m&quot;, -months, endDate)
End Function
&#x27; 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(&quot;d&quot;, -7, eventDate)
    oneDayBefore = DateAdd(&quot;d&quot;, -1, eventDate)
    oneHourBefore = DateAdd(&quot;h&quot;, -1, eventDate)
    &#x27; 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) &lt; Abs(days)
        result = DateAdd(&quot;d&quot;, direction, result)
        &#x27; Skip weekends
        If Weekday(result) &lt;&gt; vbSaturday And Weekday(result) &lt;&gt; 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
&#x27; Generate 12 month-end dates
Dim monthEnds As Variant
monthEnds = GenerateDateSeries(#1/31/2025#, &quot;m&quot;, 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
    &#x27; Calculate start of quarter
    quarterStart = DateAdd(&quot;m&quot;, (quarter - 1) * 3, fiscalYearStart)
    &#x27; End is 3 months later minus 1 day
    quarterEnd = DateAdd(&quot;d&quot;, -1, DateAdd(&quot;m&quot;, 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 &quot;daily&quot;
            GetNextOccurrence = DateAdd(&quot;d&quot;, 1, lastOccurrence)
        Case &quot;weekly&quot;
            GetNextOccurrence = DateAdd(&quot;ww&quot;, 1, lastOccurrence)
        Case &quot;biweekly&quot;
            GetNextOccurrence = DateAdd(&quot;ww&quot;, 2, lastOccurrence)
        Case &quot;monthly&quot;
            GetNextOccurrence = DateAdd(&quot;m&quot;, 1, lastOccurrence)
        Case &quot;quarterly&quot;
            GetNextOccurrence = DateAdd(&quot;q&quot;, 1, lastOccurrence)
        Case &quot;annually&quot;
            GetNextOccurrence = DateAdd(&quot;yyyy&quot;, 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
    &#x27; Simple timezone conversion (doesn&#x27;t account for DST)
    ConvertToTimeZone = DateAdd(&quot;h&quot;, hourOffset, localTime)
End Function
&#x27; 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
    &#x27; Calculate years
    tempDate = birthDate
    years = 0
    Do While DateAdd(&quot;yyyy&quot;, years + 1, tempDate) &lt;= Date
        years = years + 1
    Loop
    &#x27; Calculate remaining months
    tempDate = DateAdd(&quot;yyyy&quot;, years, birthDate)
    months = 0
    Do While DateAdd(&quot;m&quot;, months + 1, tempDate) &lt;= Date
        months = months + 1
    Loop
    &#x27; Calculate remaining days
    tempDate = DateAdd(&quot;m&quot;, months, DateAdd(&quot;yyyy&quot;, years, birthDate))
    days = DateDiff(&quot;d&quot;, tempDate, Date)
    GetExactAge = years &amp; &quot; years, &quot; &amp; months &amp; &quot; months, &quot; &amp; days &amp; &quot; days&quot;
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
    &#x27; Validate interval
    Select Case LCase(interval)
        Case &quot;yyyy&quot;, &quot;q&quot;, &quot;m&quot;, &quot;y&quot;, &quot;d&quot;, &quot;w&quot;, &quot;ww&quot;, &quot;h&quot;, &quot;n&quot;, &quot;s&quot;
            SafeDateAdd = DateAdd(interval, number, dateValue)
        Case Else
            SafeDateAdd = Null  &#x27; Invalid interval
    End Select
    Exit Function
ErrorHandler:
    SafeDateAdd = Null  &#x27; 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">&#x27; Define constants for clarity
Const INTERVAL_YEAR As String = &quot;yyyy&quot;
Const INTERVAL_MONTH As String = &quot;m&quot;
Const INTERVAL_DAY As String = &quot;d&quot;
Const INTERVAL_HOUR As String = &quot;h&quot;
&#x27; 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, , &quot;Invalid date&quot;
    End If
    AddDaysToDate = DateAdd(&quot;d&quot;, 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">&#x27; Be aware of month-end behavior
Dim jan31 As Date
jan31 = #1/31/2025#
&#x27; Adding 1 month gives Feb 28 (or 29)
Dim result As Date
result = DateAdd(&quot;m&quot;, 1, jan31)  &#x27; Feb 28, 2025
&#x27; Adding 2 months gives Mar 31
result = DateAdd(&quot;m&quot;, 2, jan31)  &#x27; 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">&#x27; DateAdd - Adds interval to date, returns new date
Dim future As Date
future = DateAdd(&quot;d&quot;, 30, Date)
&#x27; DateDiff - Calculates interval between dates, returns number
Dim difference As Long
difference = DateDiff(&quot;d&quot;, Date, future)  &#x27; Returns 30</code></pre>
<h3 id="dateadd-vs-simple-arithmetic"><code>DateAdd</code> vs Simple Arithmetic</h3>
<pre><code class="language-vbnet">&#x27; Simple arithmetic works for days
Dim tomorrow As Date
tomorrow = Date + 1  &#x27; Same as DateAdd(&quot;d&quot;, 1, Date)
&#x27; But DateAdd is better for months/years
Dim nextMonth As Date
nextMonth = DateAdd(&quot;m&quot;, 1, Date)  &#x27; 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>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>