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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<!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 - datepart - Datetime">
    <title>datepart - 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> / datepart</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="datepart-function">DatePart Function</h1>
<p>Returns a <code>Variant</code> (<code>Integer</code>) containing the specified part of a given date.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">DatePart(interval, date[, 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 return.
  See the Interval Settings section for valid values.</li>
<li><strong>date</strong>: Required. <code>Variant</code> (<code>Date</code>) value that you want to evaluate.</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>
<th>Return Range</th>
</tr>
</thead>
<tbody>
<tr>
<td>"yyyy"</td>
<td>Year</td>
<td>100-9999</td>
</tr>
<tr>
<td>"q"</td>
<td>Quarter</td>
<td>1-4</td>
</tr>
<tr>
<td>"m"</td>
<td>Month</td>
<td>1-12</td>
</tr>
<tr>
<td>"y"</td>
<td>Day of year</td>
<td>1-366</td>
</tr>
<tr>
<td>"d"</td>
<td>Day</td>
<td>1-31</td>
</tr>
<tr>
<td>"w"</td>
<td>Weekday</td>
<td>1-7 (Sunday=1)</td>
</tr>
<tr>
<td>"ww"</td>
<td>Week of year</td>
<td>1-53</td>
</tr>
<tr>
<td>"h"</td>
<td>Hour</td>
<td>0-23</td>
</tr>
<tr>
<td>"n"</td>
<td>Minute</td>
<td>0-59</td>
</tr>
<tr>
<td>"s"</td>
<td>Second</td>
<td>0-59</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 an <code>Integer</code> representing the specified part of the date. Returns <code>Null</code> if the date is <code>Null</code>.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>DatePart</code> function is used to extract a specific component from a date value.
It's particularly useful for date-based calculations, filtering, and grouping operations.
<strong>Important Characteristics:</strong>
- More flexible than <code>Year()</code>, <code>Month()</code>, or <code>Day()</code> functions.
- Can extract quarter, week, and day of year.
- Weekday numbering depends on <code>firstdayofweek</code> parameter.
- Week numbering depends on <code>firstweekofyear</code> parameter.
- Hours use 24-hour format (0-23).
- Sunday is 1 by default for weekday ("w").
- Compatible with SQL Server's <code>DATEPART</code> function</p>
<h2 id="equivalent-simple-functions">Equivalent Simple Functions</h2>
<p>Some intervals have equivalent dedicated functions:
- <code>DatePart("yyyy", date)</code> = <code>Year(date)</code>
- <code>DatePart("m", date)</code> = <code>Month(date)</code>
- <code>DatePart("d", date)</code> = <code>Day(date)</code>
- <code>DatePart("w", date)</code> = <code>Weekday(date)</code>
- <code>DatePart("h", date)</code> = <code>Hour(date)</code>
- <code>DatePart("n", date)</code> = <code>Minute(date)</code>
- <code>DatePart("s", date)</code> = <code>Second(date)</code></p>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">Dim testDate As Date
testDate = #3/15/2025 14:30:45#
&#x27; Extract various parts
MsgBox &quot;Year: &quot; &amp; DatePart(&quot;yyyy&quot;, testDate)      &#x27; 2025
MsgBox &quot;Quarter: &quot; &amp; DatePart(&quot;q&quot;, testDate)      &#x27; 1
MsgBox &quot;Month: &quot; &amp; DatePart(&quot;m&quot;, testDate)        &#x27; 3
MsgBox &quot;Day: &quot; &amp; DatePart(&quot;d&quot;, testDate)          &#x27; 15
MsgBox &quot;Day of Year: &quot; &amp; DatePart(&quot;y&quot;, testDate)  &#x27; 74
MsgBox &quot;Weekday: &quot; &amp; DatePart(&quot;w&quot;, testDate)      &#x27; Varies by day
MsgBox &quot;Week: &quot; &amp; DatePart(&quot;ww&quot;, testDate)        &#x27; Week number
MsgBox &quot;Hour: &quot; &amp; DatePart(&quot;h&quot;, testDate)         &#x27; 14
MsgBox &quot;Minute: &quot; &amp; DatePart(&quot;n&quot;, testDate)       &#x27; 30
MsgBox &quot;Second: &quot; &amp; DatePart(&quot;s&quot;, testDate)       &#x27; 45</code></pre>
<h3 id="quarter-calculation">Quarter Calculation</h3>
<pre><code class="language-vbnet">Function GetQuarter(dateValue As Date) As Integer
    GetQuarter = DatePart(&quot;q&quot;, dateValue)
End Function
&#x27; Usage
Dim currentQuarter As Integer
currentQuarter = GetQuarter(Date)
MsgBox &quot;We are in Q&quot; &amp; currentQuarter</code></pre>
<h3 id="week-number">Week Number</h3>
<pre><code class="language-vbnet">Function GetWeekNumber(dateValue As Date) As Integer
    &#x27; ISO week number (Monday start, 4-day rule)
    GetWeekNumber = DatePart(&quot;ww&quot;, dateValue, vbMonday, vbFirstFourDays)
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="fiscal-quarter-determination">Fiscal Quarter Determination</h3>
<pre><code class="language-vbnet">Function GetFiscalQuarter(dateValue As Date, fiscalYearStart As Integer) As Integer
    &#x27; fiscalYearStart is the month number (e.g., 4 for April)
    Dim currentMonth As Integer
    Dim adjustedMonth As Integer
    currentMonth = DatePart(&quot;m&quot;, dateValue)
    adjustedMonth = currentMonth - fiscalYearStart + 1
    If adjustedMonth &lt;= 0 Then
        adjustedMonth = adjustedMonth + 12
    End If
    GetFiscalQuarter = Int((adjustedMonth - 1) / 3) + 1
End Function</code></pre>
<h3 id="group-by-time-period">Group By Time Period</h3>
<pre><code class="language-vbnet">Function GroupByPeriod(dateValue As Date, period As String) As String
    Select Case LCase(period)
        Case &quot;year&quot;
            GroupByPeriod = CStr(DatePart(&quot;yyyy&quot;, dateValue))
        Case &quot;quarter&quot;
            GroupByPeriod = DatePart(&quot;yyyy&quot;, dateValue) &amp; &quot;-Q&quot; &amp; DatePart(&quot;q&quot;, dateValue)
        Case &quot;month&quot;
            GroupByPeriod = DatePart(&quot;yyyy&quot;, dateValue) &amp; &quot;-&quot; &amp; Format(DatePart(&quot;m&quot;, dateValue), &quot;00&quot;)
        Case &quot;week&quot;
            GroupByPeriod = DatePart(&quot;yyyy&quot;, dateValue) &amp; &quot;-W&quot; &amp; Format(DatePart(&quot;ww&quot;, dateValue), &quot;00&quot;)
        Case Else
            GroupByPeriod = Format(dateValue, &quot;yyyy-mm-dd&quot;)
    End Select
End Function</code></pre>
<h3 id="day-name-from-weekday">Day Name from Weekday</h3>
<pre><code class="language-vbnet">Function GetDayName(dateValue As Date) As String
    Select Case DatePart(&quot;w&quot;, dateValue)
        Case 1: GetDayName = &quot;Sunday&quot;
        Case 2: GetDayName = &quot;Monday&quot;
        Case 3: GetDayName = &quot;Tuesday&quot;
        Case 4: GetDayName = &quot;Wednesday&quot;
        Case 5: GetDayName = &quot;Thursday&quot;
        Case 6: GetDayName = &quot;Friday&quot;
        Case 7: GetDayName = &quot;Saturday&quot;
    End Select
End Function</code></pre>
<h3 id="time-of-day-category">Time of Day Category</h3>
<pre><code class="language-vbnet">Function GetTimeOfDay(dateValue As Date) As String
    Dim hour As Integer
    hour = DatePart(&quot;h&quot;, dateValue)
    Select Case hour
        Case 0 To 5
            GetTimeOfDay = &quot;Night&quot;
        Case 6 To 11
            GetTimeOfDay = &quot;Morning&quot;
        Case 12 To 17
            GetTimeOfDay = &quot;Afternoon&quot;
        Case 18 To 23
            GetTimeOfDay = &quot;Evening&quot;
    End Select
End Function</code></pre>
<h3 id="business-hour-check">Business Hour Check</h3>
<pre><code class="language-vbnet">Function IsBusinessHours(checkTime As Date) As Boolean
    Dim hour As Integer
    Dim weekday As Integer
    hour = DatePart(&quot;h&quot;, checkTime)
    weekday = DatePart(&quot;w&quot;, checkTime)
    &#x27; Monday-Friday, 9 AM - 5 PM
    If weekday &gt;= 2 And weekday &lt;= 6 Then  &#x27; Mon-Fri
        If hour &gt;= 9 And hour &lt; 17 Then
            IsBusinessHours = True
        End If
    End If
End Function</code></pre>
<h3 id="month-name-lookup">Month Name Lookup</h3>
<pre><code class="language-vbnet">Function GetMonthName(dateValue As Date) As String
    Dim monthNames As Variant
    Dim monthNum As Integer
    monthNames = Array(&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, _
                      &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;)
    monthNum = DatePart(&quot;m&quot;, dateValue)
    GetMonthName = monthNames(monthNum - 1)
End Function</code></pre>
<h3 id="quarter-end-date">Quarter End Date</h3>
<pre><code class="language-vbnet">Function GetQuarterEnd(dateValue As Date) As Date
    Dim quarter As Integer
    Dim year As Integer
    Dim endMonth As Integer
    quarter = DatePart(&quot;q&quot;, dateValue)
    year = DatePart(&quot;yyyy&quot;, dateValue)
    endMonth = quarter * 3
    GetQuarterEnd = DateSerial(year, endMonth + 1, 0)  &#x27; Last day of quarter
End Function</code></pre>
<h3 id="data-binning-by-hour">Data Binning by Hour</h3>
<pre><code class="language-vbnet">Function GetHourBucket(timestamp As Date) As String
    Dim hour As Integer
    hour = DatePart(&quot;h&quot;, timestamp)
    GetHourBucket = Format(hour, &quot;00&quot;) &amp; &quot;:00&quot;
End Function
&#x27; Use for grouping log entries
Sub AnalyzeLogs()
    Dim entry As Date
    Dim bucket As String
    For Each entry In logEntries
        bucket = GetHourBucket(entry)
        hourCounts(bucket) = hourCounts(bucket) + 1
    Next
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="iso-8601-week-number">ISO 8601 Week Number</h3>
<pre><code class="language-vbnet">Function GetISOWeekNumber(dateValue As Date) As Integer
    &#x27; ISO 8601 week number: Monday start, 4-day rule
    GetISOWeekNumber = DatePart(&quot;ww&quot;, dateValue, vbMonday, vbFirstFourDays)
End Function
Function GetISOYear(dateValue As Date) As Integer
    &#x27; Year for ISO week (may differ from calendar year)
    Dim weekNum As Integer
    Dim month As Integer
    weekNum = GetISOWeekNumber(dateValue)
    month = DatePart(&quot;m&quot;, dateValue)
    If month = 1 And weekNum &gt; 51 Then
        GetISOYear = DatePart(&quot;yyyy&quot;, dateValue) - 1
    ElseIf month = 12 And weekNum = 1 Then
        GetISOYear = DatePart(&quot;yyyy&quot;, dateValue) + 1
    Else
        GetISOYear = DatePart(&quot;yyyy&quot;, dateValue)
    End If
End Function</code></pre>
<h3 id="dynamic-date-grouping">Dynamic Date Grouping</h3>
<pre><code class="language-vbnet">Function GetDateKey(dateValue As Date, granularity As String) As String
    Dim year As Integer
    Dim month As Integer
    Dim day As Integer
    Dim week As Integer
    Dim quarter As Integer
    year = DatePart(&quot;yyyy&quot;, dateValue)
    Select Case LCase(granularity)
        Case &quot;year&quot;
            GetDateKey = CStr(year)
        Case &quot;quarter&quot;
            quarter = DatePart(&quot;q&quot;, dateValue)
            GetDateKey = year &amp; &quot;Q&quot; &amp; quarter
        Case &quot;month&quot;
            month = DatePart(&quot;m&quot;, dateValue)
            GetDateKey = year &amp; Format(month, &quot;00&quot;)
        Case &quot;week&quot;
            week = DatePart(&quot;ww&quot;, dateValue, vbMonday)
            GetDateKey = year &amp; &quot;W&quot; &amp; Format(week, &quot;00&quot;)
        Case &quot;day&quot;
            month = DatePart(&quot;m&quot;, dateValue)
            day = DatePart(&quot;d&quot;, dateValue)
            GetDateKey = year &amp; Format(month, &quot;00&quot;) &amp; Format(day, &quot;00&quot;)
        Case Else
            GetDateKey = Format(dateValue, &quot;yyyymmdd&quot;)
    End Select
End Function</code></pre>
<h3 id="custom-calendar-system">Custom Calendar System</h3>
<pre><code class="language-vbnet">Type CustomCalendar
    Year As Integer
    Period As Integer
    Week As Integer
    Day As Integer
End Type
Function ConvertToCustomCalendar(dateValue As Date) As CustomCalendar
    Dim cal As CustomCalendar
    Dim yearStart As Date
    Dim dayOfYear As Integer
    cal.Year = DatePart(&quot;yyyy&quot;, dateValue)
    &#x27; 13 periods of 4 weeks each
    yearStart = DateSerial(cal.Year, 1, 1)
    dayOfYear = DatePart(&quot;y&quot;, dateValue)
    cal.Week = Int((dayOfYear - 1) / 7) + 1
    cal.Period = Int((cal.Week - 1) / 4) + 1
    cal.Day = DatePart(&quot;w&quot;, dateValue, vbMonday)
    ConvertToCustomCalendar = cal
End Function</code></pre>
<h3 id="time-series-aggregation">Time Series Aggregation</h3>
<pre><code class="language-vbnet">Function AggregateByInterval(dates() As Date, values() As Double, _
                            interval As String) As Collection
    Dim results As New Collection
    Dim i As Long
    Dim key As String
    Dim total As Double
    Dim count As Long
    For i = LBound(dates) To UBound(dates)
        key = GetDateKey(dates(i), interval)
        On Error Resume Next
        total = results(key)
        If Err.Number &lt;&gt; 0 Then
            results.Add values(i), key
        Else
            results.Remove key
            results.Add total + values(i), key
        End If
        On Error GoTo 0
    Next i
    Set AggregateByInterval = results
End Function</code></pre>
<h3 id="shift-schedule-detector">Shift Schedule Detector</h3>
<pre><code class="language-vbnet">Function GetShift(timestamp As Date) As String
    Dim hour As Integer
    Dim weekday As Integer
    hour = DatePart(&quot;h&quot;, timestamp)
    weekday = DatePart(&quot;w&quot;, timestamp)
    &#x27; Weekend check
    If weekday = 1 Or weekday = 7 Then
        GetShift = &quot;Weekend&quot;
        Exit Function
    End If
    &#x27; Shift determination
    Select Case hour
        Case 6 To 13
            GetShift = &quot;Morning Shift&quot;
        Case 14 To 21
            GetShift = &quot;Afternoon Shift&quot;
        Case Else
            GetShift = &quot;Night Shift&quot;
    End Select
End Function</code></pre>
<h3 id="calendar-week-display">Calendar Week Display</h3>
<pre><code class="language-vbnet">Function FormatCalendarWeek(dateValue As Date, Optional useISO As Boolean = False) As String
    Dim year As Integer
    Dim week As Integer
    If useISO Then
        year = GetISOYear(dateValue)
        week = GetISOWeekNumber(dateValue)
    Else
        year = DatePart(&quot;yyyy&quot;, dateValue)
        week = DatePart(&quot;ww&quot;, dateValue)
    End If
    FormatCalendarWeek = year &amp; &quot;-W&quot; &amp; Format(week, &quot;00&quot;)
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeDatePart(interval As String, dateValue As Variant) As Variant
    On Error GoTo ErrorHandler
    &#x27; Validate date
    If Not IsDate(dateValue) Then
        SafeDatePart = 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;
            SafeDatePart = DatePart(interval, CDate(dateValue))
        Case Else
            SafeDatePart = Null
    End Select
    Exit Function
ErrorHandler:
    SafeDatePart = 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 value passed as date parameter</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>DatePart</code> is efficient for single extractions</li>
<li>For multiple parts from same date, consider using dedicated functions:</li>
</ul>
<pre><code class="language-vbnet">  &#x27; Less efficient
  y = DatePart(&quot;yyyy&quot;, d)
  m = DatePart(&quot;m&quot;, d)
  d = DatePart(&quot;d&quot;, d)
  &#x27; More efficient
  y = Year(d)
  m = Month(d)
  d = Day(d)
  ```
- Week calculations are more expensive than other intervals
- Cache results when processing large datasets
## Best Practices
### Use Named Constants</code></pre>
<p>vb
' Define interval constants
Const INTERVAL_YEAR As String = "yyyy"
Const INTERVAL_QUARTER As String = "q"
Const INTERVAL_MONTH As String = "m"
Const INTERVAL_WEEK As String = "ww"
' Use in code
quarter = DatePart(INTERVAL_QUARTER, Date)</p>
<pre><code>### Prefer Specific Functions for Simple Cases</code></pre>
<p>vb
' Good - Use specific function
y = Year(someDate)
' Less clear - Using DatePart
y = DatePart("yyyy", someDate)</p>
<pre><code>### Be Aware of Weekday Numbering</code></pre>
<p>vb
' Default: Sunday = 1
day = DatePart("w", Date)
' Explicit: Monday = 1
day = DatePart("w", Date, vbMonday)</p>
<pre><code>## Comparison with Other Functions
### `DatePart` vs Dedicated Functions</code></pre>
<p>vb
' DatePart - Flexible, supports all intervals
quarter = DatePart("q", Date)
dayOfYear = DatePart("y", Date)
' Dedicated - Simpler, more readable for common cases
year = Year(Date)
month = Month(Date)
day = Day(Date)
weekday = Weekday(Date)
```</p>
<h2 id="limitations">Limitations</h2>
<ul>
<li>No millisecond support</li>
<li>Week numbering can be confusing with different standards (ISO vs US)</li>
<li>Quarter calculation doesn't support fiscal quarters directly</li>
<li>No built-in locale-aware day/month names</li>
<li><code>FirstWeekOfYear</code> affects week numbering interpretation</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Year</code>: Returns the year part of a date</li>
<li><code>Month</code>: Returns the month part of a date</li>
<li><code>Day</code>: Returns the day part of a date</li>
<li><code>Weekday</code>: Returns the day of the week</li>
<li><code>Hour</code>: Returns the hour part of a time</li>
<li><code>Minute</code>: Returns the minute part of a time</li>
<li><code>Second</code>: Returns the second part of a time</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>DateSerial</code>: Creates a date from year, month, and day values</li>
<li><code>Format</code>: Formats a date as a string (alternative for custom formatting)</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>