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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<!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 - datediff - Datetime">
    <title>datediff - 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> / datediff</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="datediff-function">DateDiff Function</h1>
<p>Returns a <code>Variant</code> (<code>Long</code>) specifying the number of time intervals between two specified dates.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>interval</strong>: Required. <code>String</code> expression that is the interval of time you want to use
  to calculate the difference between date1 and date2. See Interval Settings for values.</li>
<li><strong>date1</strong>, <strong>date2</strong>: Required. <code>Variant</code> (<code>Date</code>) values that you want to use in the calculation.</li>
<li><strong>firstdayofweek</strong>: Optional. Constant that specifies the first day of the week.
  If not specified, Sunday is assumed. See <code>FirstDayOfWeek</code> Constants.</li>
<li><strong>firstweekofyear</strong>: Optional. Constant that specifies the first week of the year.
  If not specified, the first week is assumed to be the week containing January 1.
  See <code>FirstWeekOfYear</code> Constants.</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="firstdayofweek-constants"><code>FirstDayOfWeek</code> Constants</h2>
<table>
<thead>
<tr>
<th>Constant</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>vbUseSystem</td>
<td>0</td>
<td>Use system setting</td>
</tr>
<tr>
<td>vbSunday</td>
<td>1</td>
<td>Sunday (default)</td>
</tr>
<tr>
<td>vbMonday</td>
<td>2</td>
<td>Monday</td>
</tr>
<tr>
<td>vbTuesday</td>
<td>3</td>
<td>Tuesday</td>
</tr>
<tr>
<td>vbWednesday</td>
<td>4</td>
<td>Wednesday</td>
</tr>
<tr>
<td>vbThursday</td>
<td>5</td>
<td>Thursday</td>
</tr>
<tr>
<td>vbFriday</td>
<td>6</td>
<td>Friday</td>
</tr>
<tr>
<td>vbSaturday</td>
<td>7</td>
<td>Saturday</td>
</tr>
</tbody>
</table>
<h2 id="firstweekofyear-constants"><code>FirstWeekOfYear</code> Constants</h2>
<table>
<thead>
<tr>
<th>Constant</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>vbUseSystem</td>
<td>0</td>
<td>Use system setting</td>
</tr>
<tr>
<td>vbFirstJan1</td>
<td>1</td>
<td>Start with week containing January 1 (default)</td>
</tr>
<tr>
<td>vbFirstFourDays</td>
<td>2</td>
<td>Start with week having at least 4 days in new year</td>
</tr>
<tr>
<td>vbFirstFullWeek</td>
<td>3</td>
<td>Start with first full week of the year</td>
</tr>
</tbody>
</table>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Long</code> integer representing the number of intervals between the two dates.
The result is positive if date2 is later than date1, negative if date2 is earlier than date1,
and zero if they are equal.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>DateDiff</code> function is used to calculate the difference between two dates in the
specified time interval. The function counts the number of interval boundaries crossed
between the two dates.
<strong>Important Characteristics:</strong>
- Returns positive number if date2 &gt; date1 (future date)
- Returns negative number if date2 &lt; date1 (past date)
- Returns zero if date2 = date1 (same date/time)
- Counts interval boundaries, not elapsed time
- For "yyyy", crossing from Dec 31 to Jan 1 counts as 1 year
- For "m", crossing from Jan 31 to Feb 1 counts as 1 month
- For "ww", counts week boundaries (Sunday to Sunday by default)
- Day of year ("y") is equivalent to day ("d")
- Weekday ("w") is equivalent to day ("d")</p>
<h2 id="boundary-counting-vs-elapsed-time">Boundary Counting vs Elapsed Time</h2>
<p><code>DateDiff</code> counts boundaries crossed, not elapsed time:</p>
<pre><code class="language-vbnet">&#x27; Year example
DateDiff(&quot;yyyy&quot;, #12/31/2024#, #1/1/2025#)  &#x27; Returns 1 (crossed 1 year boundary)
&#x27; But only 1 day elapsed!
&#x27; Month example
DateDiff(&quot;m&quot;, #1/31/2025#, #2/1/2025#)  &#x27; Returns 1 (crossed 1 month boundary)
&#x27; But only 1 day elapsed!
&#x27; Day example (actual elapsed time)
DateDiff(&quot;d&quot;, #1/1/2025#, #1/31/2025#)  &#x27; Returns 30 (30 days elapsed)</code></pre>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">&#x27; Calculate days between dates
Dim days As Long
days = DateDiff(&quot;d&quot;, #1/1/2025#, #1/31/2025#)
MsgBox &quot;Days: &quot; &amp; days  &#x27; Shows 30
&#x27; Calculate months between dates
Dim months As Long
months = DateDiff(&quot;m&quot;, #1/15/2025#, #6/15/2025#)
MsgBox &quot;Months: &quot; &amp; months  &#x27; Shows 5
&#x27; Calculate years between dates
Dim years As Long
years = DateDiff(&quot;yyyy&quot;, #1/1/2000#, #1/1/2025#)
MsgBox &quot;Years: &quot; &amp; years  &#x27; Shows 25</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 = DateDiff(&quot;yyyy&quot;, birthDate, Date)
    &#x27; Adjust if birthday hasn&#x27;t occurred this year
    If DateSerial(Year(Date), Month(birthDate), Day(birthDate)) &gt; Date Then
        age = age - 1
    End If
    CalculateAge = age
End Function</code></pre>
<h3 id="days-untilsince-event">Days Until/Since Event</h3>
<pre><code class="language-vbnet">Function DaysUntilEvent(eventDate As Date) As Long
    DaysUntilEvent = DateDiff(&quot;d&quot;, Date, eventDate)
End Function
&#x27; Usage
Dim daysLeft As Long
daysLeft = DaysUntilEvent(#12/25/2025#)
If daysLeft &gt; 0 Then
    MsgBox daysLeft &amp; &quot; days until Christmas&quot;
ElseIf daysLeft &lt; 0 Then
    MsgBox &quot;Christmas was &quot; &amp; Abs(daysLeft) &amp; &quot; days ago&quot;
Else
    MsgBox &quot;Today is Christmas!&quot;
End If</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="elapsed-time-display">Elapsed Time Display</h3>
<pre><code class="language-vbnet">Function FormatElapsedTime(startTime As Date, endTime As Date) As String
    Dim hours As Long
    Dim minutes As Long
    Dim seconds As Long
    hours = DateDiff(&quot;h&quot;, startTime, endTime)
    minutes = DateDiff(&quot;n&quot;, startTime, endTime) Mod 60
    seconds = DateDiff(&quot;s&quot;, startTime, endTime) Mod 60
    FormatElapsedTime = hours &amp; &quot;:&quot; &amp; Format(minutes, &quot;00&quot;) &amp; &quot;:&quot; &amp; Format(seconds, &quot;00&quot;)
End Function</code></pre>
<h3 id="working-days-calculator">Working Days Calculator</h3>
<pre><code class="language-vbnet">Function CountWorkingDays(startDate As Date, endDate As Date) As Long
    Dim dayCount As Long
    Dim workDays As Long
    Dim currentDate As Date
    dayCount = DateDiff(&quot;d&quot;, startDate, endDate)
    workDays = 0
    For i = 0 To dayCount
        currentDate = DateAdd(&quot;d&quot;, i, startDate)
        If Weekday(currentDate) &lt;&gt; vbSaturday And Weekday(currentDate) &lt;&gt; vbSunday Then
            workDays = workDays + 1
        End If
    Next i
    CountWorkingDays = workDays
End Function</code></pre>
<h3 id="overdue-indicator">Overdue Indicator</h3>
<pre><code class="language-vbnet">Function GetOverdueDays(dueDate As Date) As Long
    Dim days As Long
    days = DateDiff(&quot;d&quot;, dueDate, Date)
    If days &gt; 0 Then
        GetOverdueDays = days  &#x27; Positive = overdue
    Else
        GetOverdueDays = 0     &#x27; Not overdue
    End If
End Function</code></pre>
<h3 id="subscription-status">Subscription Status</h3>
<pre><code class="language-vbnet">Function GetSubscriptionStatus(startDate As Date, endDate As Date) As String
    Dim daysRemaining As Long
    daysRemaining = DateDiff(&quot;d&quot;, Date, endDate)
    Select Case daysRemaining
        Case Is &lt; 0
            GetSubscriptionStatus = &quot;Expired&quot;
        Case 0 To 7
            GetSubscriptionStatus = &quot;Expiring Soon (&quot; &amp; daysRemaining &amp; &quot; days)&quot;
        Case 8 To 30
            GetSubscriptionStatus = &quot;Active (&quot; &amp; daysRemaining &amp; &quot; days left)&quot;
        Case Else
            GetSubscriptionStatus = &quot;Active&quot;
    End Select
End Function</code></pre>
<h3 id="quarterly-report-period">Quarterly Report Period</h3>
<pre><code class="language-vbnet">Function GetQuartersBetween(startDate As Date, endDate As Date) As Integer
    GetQuartersBetween = DateDiff(&quot;q&quot;, startDate, endDate)
End Function
&#x27; Check if in same quarter
Function InSameQuarter(date1 As Date, date2 As Date) As Boolean
    InSameQuarter = (DateDiff(&quot;q&quot;, date1, date2) = 0)
End Function</code></pre>
<h3 id="meeting-interval-tracker">Meeting Interval Tracker</h3>
<pre><code class="language-vbnet">Function WeeksSinceLastMeeting(lastMeeting As Date) As Long
    WeeksSinceLastMeeting = DateDiff(&quot;ww&quot;, lastMeeting, Date)
End Function
Function IsMeetingDue(lastMeeting As Date, interval As Integer) As Boolean
    IsMeetingDue = (DateDiff(&quot;ww&quot;, lastMeeting, Date) &gt;= interval)
End Function</code></pre>
<h3 id="time-tracking">Time Tracking</h3>
<pre><code class="language-vbnet">Sub LogSessionDuration(startTime As Date, endTime As Date)
    Dim hours As Long
    Dim minutes As Long
    hours = DateDiff(&quot;h&quot;, startTime, endTime)
    minutes = DateDiff(&quot;n&quot;, startTime, endTime) - (hours * 60)
    Debug.Print &quot;Session duration: &quot; &amp; hours &amp; &quot;h &quot; &amp; minutes &amp; &quot;m&quot;
End Sub</code></pre>
<h3 id="age-range-categorization">Age Range Categorization</h3>
<pre><code class="language-vbnet">Function GetAgeCategory(birthDate As Date) As String
    Dim age As Integer
    age = DateDiff(&quot;yyyy&quot;, birthDate, Date)
    &#x27; Adjust for birthday not yet occurred
    If Month(Date) &lt; Month(birthDate) Or _
       (Month(Date) = Month(birthDate) And Day(Date) &lt; Day(birthDate)) Then
        age = age - 1
    End If
    Select Case age
        Case 0 To 12
            GetAgeCategory = &quot;Child&quot;
        Case 13 To 19
            GetAgeCategory = &quot;Teenager&quot;
        Case 20 To 64
            GetAgeCategory = &quot;Adult&quot;
        Case Else
            GetAgeCategory = &quot;Senior&quot;
    End Select
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="complete-time-breakdown">Complete Time Breakdown</h3>
<pre><code class="language-vbnet">Type TimeBreakdown
    Years As Long
    Months As Long
    Days As Long
    Hours As Long
    Minutes As Long
    Seconds As Long
End Type
Function GetDetailedDifference(startDate As Date, endDate As Date) As TimeBreakdown
    Dim result As TimeBreakdown
    Dim tempDate As Date
    &#x27; Calculate years
    result.Years = DateDiff(&quot;yyyy&quot;, startDate, endDate)
    tempDate = DateAdd(&quot;yyyy&quot;, result.Years, startDate)
    If tempDate &gt; endDate Then
        result.Years = result.Years - 1
        tempDate = DateAdd(&quot;yyyy&quot;, result.Years, startDate)
    End If
    &#x27; Calculate months
    result.Months = DateDiff(&quot;m&quot;, tempDate, endDate)
    tempDate = DateAdd(&quot;m&quot;, result.Months, tempDate)
    If tempDate &gt; endDate Then
        result.Months = result.Months - 1
        tempDate = DateAdd(&quot;m&quot;, result.Months, DateAdd(&quot;yyyy&quot;, result.Years, startDate))
    End If
    &#x27; Calculate remaining time
    result.Days = DateDiff(&quot;d&quot;, tempDate, endDate)
    result.Hours = DateDiff(&quot;h&quot;, tempDate, endDate) Mod 24
    result.Minutes = DateDiff(&quot;n&quot;, tempDate, endDate) Mod 60
    result.Seconds = DateDiff(&quot;s&quot;, tempDate, endDate) Mod 60
    GetDetailedDifference = result
End Function</code></pre>
<h3 id="week-number-with-custom-first-day">Week Number with Custom First Day</h3>
<pre><code class="language-vbnet">Function GetWeekNumber(dateValue As Date, startDay As VbDayOfWeek) As Long
    Dim yearStart As Date
    yearStart = DateSerial(Year(dateValue), 1, 1)
    GetWeekNumber = DateDiff(&quot;ww&quot;, yearStart, dateValue, startDay, vbFirstFourDays)
End Function
&#x27; Usage
Dim weekNum As Long
weekNum = GetWeekNumber(Date, vbMonday)  &#x27; ISO week number (Monday start)</code></pre>
<h3 id="performance-timer">Performance Timer</h3>
<pre><code class="language-vbnet">Private m_startTime As Date
Sub StartTimer()
    m_startTime = Now
End Sub
Function GetElapsedMilliseconds() As Double
    Dim seconds As Long
    seconds = DateDiff(&quot;s&quot;, m_startTime, Now)
    &#x27; VB6 doesn&#x27;t support milliseconds directly
    &#x27; This gives seconds as closest approximation
    GetElapsedMilliseconds = seconds * 1000
End Function</code></pre>
<h3 id="date-range-validator">Date Range Validator</h3>
<pre><code class="language-vbnet">Function ValidateDateRange(startDate As Date, endDate As Date, _
                         maxDays As Long) As Boolean
    Dim daysDiff As Long
    &#x27; Check date order
    If startDate &gt; endDate Then
        ValidateDateRange = False
        Exit Function
    End If
    &#x27; Check range limit
    daysDiff = DateDiff(&quot;d&quot;, startDate, endDate)
    ValidateDateRange = (daysDiff &lt;= maxDays)
End Function</code></pre>
<h3 id="fiscal-period-calculator">Fiscal Period Calculator</h3>
<pre><code class="language-vbnet">Function GetFiscalPeriodDifference(date1 As Date, date2 As Date, _
                                  fiscalYearStart As Integer) As Long
    &#x27; Calculate fiscal months between dates
    &#x27; fiscalYearStart = month number (e.g., 4 for April)
    Dim adjustedDate1 As Date
    Dim adjustedDate2 As Date
    &#x27; Adjust dates to fiscal year basis
    adjustedDate1 = DateSerial(Year(date1), Month(date1) - fiscalYearStart + 1, Day(date1))
    adjustedDate2 = DateSerial(Year(date2), Month(date2) - fiscalYearStart + 1, Day(date2))
    GetFiscalPeriodDifference = DateDiff(&quot;m&quot;, adjustedDate1, adjustedDate2)
End Function</code></pre>
<h3 id="batch-date-comparison">Batch Date Comparison</h3>
<pre><code class="language-vbnet">Function FindOldestDate(dates() As Date) As Date
    Dim i As Integer
    Dim oldest As Date
    oldest = dates(LBound(dates))
    For i = LBound(dates) + 1 To UBound(dates)
        If DateDiff(&quot;d&quot;, dates(i), oldest) &gt; 0 Then
            oldest = dates(i)
        End If
    Next i
    FindOldestDate = oldest
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeDateDiff(interval As String, date1 As Variant, _
                     date2 As Variant) As Variant
    On Error GoTo ErrorHandler
    &#x27; Validate dates
    If Not IsDate(date1) Or Not IsDate(date2) Then
        SafeDateDiff = Null
        Exit Function
    End If
    &#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;
            SafeDateDiff = DateDiff(interval, CDate(date1), CDate(date2))
        Case Else
            SafeDateDiff = Null
    End Select
    Exit Function
ErrorHandler:
    SafeDateDiff = Null
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-date values passed as date parameters</li>
<li><strong>Error 6</strong> (Overflow): Result exceeds Long integer range</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>DateDiff</code> is very fast for simple interval calculations</li>
<li>Day ("d") calculations are fastest</li>
<li>Month ("m") and year ("yyyy") require more computation</li>
<li>Week calculations depend on <code>FirstDayOfWeek</code> and <code>FirstWeekOfYear</code> parameters</li>
<li>For large datasets, cache <code>DateDiff</code> results when possible</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="use-appropriate-intervals">Use Appropriate Intervals</h3>
<pre><code class="language-vbnet">&#x27; Good - Use &quot;d&quot; for exact day count
days = DateDiff(&quot;d&quot;, startDate, endDate)
&#x27; Be careful - &quot;yyyy&quot; counts year boundaries, not elapsed years
years = DateDiff(&quot;yyyy&quot;, #12/31/2024#, #1/1/2025#)  &#x27; Returns 1, but only 1 day!</code></pre>
<h3 id="order-matters">Order Matters</h3>
<pre><code class="language-vbnet">&#x27; Positive result - date2 is in future
diff = DateDiff(&quot;d&quot;, #1/1/2025#, #1/31/2025#)  &#x27; Returns 30
&#x27; Negative result - date2 is in past
diff = DateDiff(&quot;d&quot;, #1/31/2025#, #1/1/2025#)  &#x27; Returns -30</code></pre>
<h3 id="handle-negative-results">Handle Negative Results</h3>
<pre><code class="language-vbnet">Function GetAbsoluteDaysDifference(date1 As Date, date2 As Date) As Long
    GetAbsoluteDaysDifference = Abs(DateDiff(&quot;d&quot;, date1, date2))
End Function</code></pre>
<h3 id="validate-date-order">Validate Date Order</h3>
<pre><code class="language-vbnet">Function CalculateDuration(startDate As Date, endDate As Date) As Long
    If startDate &gt; endDate Then
        Err.Raise 5, , &quot;Start date must be before end date&quot;
    End If
    CalculateDuration = DateDiff(&quot;d&quot;, startDate, endDate)
End Function</code></pre>
<h2 id="comparison-with-other-functions">Comparison with Other Functions</h2>
<h3 id="datediff-vs-dateadd"><code>DateDiff</code> vs <code>DateAdd</code></h3>
<pre><code class="language-vbnet">&#x27; `DateDiff` - Calculate interval between dates (returns Long)
diff = DateDiff(&quot;d&quot;, #1/1/2025#, #1/31/2025#)  &#x27; Returns 30
&#x27; `DateAdd` - Add interval to date (returns Date)
newDate = DateAdd(&quot;d&quot;, 30, #1/1/2025#)  &#x27; Returns #1/31/2025#</code></pre>
<h3 id="datediff-vs-subtraction"><code>DateDiff</code> vs Subtraction</h3>
<pre><code class="language-vbnet">&#x27; Subtraction gives days as Double
diff = #1/31/2025# - #1/1/2025#  &#x27; Returns 30.0
&#x27; DateDiff gives days as Long
diff = DateDiff(&quot;d&quot;, #1/1/2025#, #1/31/2025#)  &#x27; Returns 30
&#x27; DateDiff supports other intervals
months = DateDiff(&quot;m&quot;, #1/1/2025#, #6/1/2025#)  &#x27; Returns 5</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Result must fit in Long integer range (-2,147,483,648 to 2,147,483,647)</li>
<li>Week calculations depend on system or specified first day of week</li>
<li>Counts boundaries crossed, not elapsed time (except for "d", "h", "n", "s")</li>
<li>No built-in support for milliseconds</li>
<li>No built-in support for business day calculations</li>
<li>Cannot directly exclude holidays</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>DateAdd</code>: Adds a time interval to a date</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>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>Weekday</code>: Returns the day of the week</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>