<!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 - year - Datetime">
<title>year - 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> / year</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">
<p>VB6 Year Function
The <code>Year</code> function returns an Integer representing the year of a specified date.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Year(date)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>date</code>: Required. Any Variant, numeric expression, string expression, or any combination that can represent a date. If <code>date</code> contains Null, Null is returned.</li>
</ul>
<h2 id="returns">Returns</h2>
<p>Returns an <code>Integer</code> representing the year (a whole number between 100 and 9999, inclusive).</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Year</code> function extracts the year component from a date value:
- <strong>Return range</strong>: Returns values from 100 to 9999
- <strong>Null handling</strong>: If the date argument is Null, the function returns Null
- <strong>Date validation</strong>: Invalid dates cause Error 13 (Type mismatch)
- <strong>String dates</strong>: Accepts string representations of dates (e.g., "12/25/2023")
- <strong>Numeric dates</strong>: Accepts numeric date values (serial dates)
- <strong>Date literals</strong>: Accepts date literals (e.g., #12/25/2023#)
- <strong>Current year</strong>: Use <code>Year(Date)</code> or <code>Year(Now)</code> to get current year
- <strong>Leap year calculation</strong>: Can be used to determine leap years
- <strong>Year arithmetic</strong>: Often combined with <code>DateSerial</code> for date calculations
- <strong>Four-digit years</strong>: Always returns full four-digit year (not two-digit)</p>
<h3 id="date-components-family">Date Components Family</h3>
<p>The <code>Year</code> function is part of a family of date component extraction functions:
- <code>Year(date)</code> - Returns the year (100-9999)
- <code>Month(date)</code> - Returns the month (1-12)
- <code>Day(date)</code> - Returns the day (1-31)
- <code>Weekday(date)</code> - Returns the day of week (1-7)
- <code>Hour(date)</code> - Returns the hour (0-23)
- <code>Minute(date)</code> - Returns the minute (0-59)
- <code>Second(date)</code> - Returns the second (0-59)</p>
<h3 id="leap-year-detection">Leap Year Detection</h3>
<pre><code class="language-vbnet">Function IsLeapYear(yr As Integer) As Boolean
IsLeapYear = ((yr Mod 4 = 0) And (yr Mod 100 <> 0)) Or (yr Mod 400 = 0)
End Function</code></pre>
<h3 id="combining-with-dateserial">Combining with <code>DateSerial</code></h3>
<pre><code class="language-vbnet">' Get first day of current year
firstDay = DateSerial(Year(Date), 1, 1)
' Get last day of current year
lastDay = DateSerial(Year(Date), 12, 31)</code></pre>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Extract Year</strong>: Get the year component from a date</li>
<li><strong>Age Calculation</strong>: Calculate age from birth date</li>
<li><strong>Fiscal Year</strong>: Determine fiscal year for financial reporting</li>
<li><strong>Year Filtering</strong>: Filter data by year</li>
<li><strong>Year Validation</strong>: Validate year ranges in data entry</li>
<li><strong>Archive Organization</strong>: Organize files/records by year</li>
<li><strong>Year Comparison</strong>: Compare dates across different years</li>
<li><strong>Report Grouping</strong>: Group data by year for reports</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<h3 id="example-1-get-current-year">Example 1: Get Current Year</h3>
<pre><code class="language-vbnet">Sub ShowCurrentYear()
Dim currentYear As Integer
currentYear = Year(Date)
MsgBox "Current year: " & currentYear
End Sub</code></pre>
<h3 id="example-2-calculate-age">Example 2: Calculate Age</h3>
<pre><code class="language-vbnet">Function CalculateAge(birthDate As Date) As Integer
Dim age As Integer
age = Year(Date) - Year(birthDate)
' Adjust if birthday hasn't occurred this year
If Month(Date) < Month(birthDate) Or _
(Month(Date) = Month(birthDate) And Day(Date) < Day(birthDate)) Then
age = age - 1
End If
CalculateAge = age
End Function</code></pre>
<h3 id="example-3-get-years-between-dates">Example 3: Get Years Between Dates</h3>
<pre><code class="language-vbnet">Function YearsBetween(startDate As Date, endDate As Date) As Integer
YearsBetween = Year(endDate) - Year(startDate)
End Function</code></pre>
<h3 id="example-4-check-if-leap-year">Example 4: Check If Leap Year</h3>
<pre><code class="language-vbnet">Function IsLeapYear(dt As Date) As Boolean
Dim yr As Integer
yr = Year(dt)
IsLeapYear = ((yr Mod 4 = 0) And (yr Mod 100 <> 0)) Or (yr Mod 400 = 0)
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-get-first-day-of-year">Pattern 1: Get First Day of Year</h3>
<pre><code class="language-vbnet">Function GetFirstDayOfYear(dt As Date) As Date
GetFirstDayOfYear = DateSerial(Year(dt), 1, 1)
End Function</code></pre>
<h3 id="pattern-2-get-last-day-of-year">Pattern 2: Get Last Day of Year</h3>
<pre><code class="language-vbnet">Function GetLastDayOfYear(dt As Date) As Date
GetLastDayOfYear = DateSerial(Year(dt), 12, 31)
End Function</code></pre>
<h3 id="pattern-3-same-year-check">Pattern 3: Same Year Check</h3>
<pre><code class="language-vbnet">Function IsSameYear(date1 As Date, date2 As Date) As Boolean
IsSameYear = (Year(date1) = Year(date2))
End Function</code></pre>
<h3 id="pattern-4-get-fiscal-year">Pattern 4: Get Fiscal Year</h3>
<pre><code class="language-vbnet">Function GetFiscalYear(dt As Date, fiscalStartMonth As Integer) As Integer
If Month(dt) >= fiscalStartMonth Then
GetFiscalYear = Year(dt)
Else
GetFiscalYear = Year(dt) - 1
End If
End Function</code></pre>
<h3 id="pattern-5-format-year-display">Pattern 5: Format Year Display</h3>
<pre><code class="language-vbnet">Function FormatYearDisplay(dt As Date) As String
FormatYearDisplay = "Year " & Year(dt)
End Function</code></pre>
<h3 id="pattern-6-year-difference">Pattern 6: Year Difference</h3>
<pre><code class="language-vbnet">Function GetYearDifference(startDate As Date, endDate As Date) As Integer
GetYearDifference = Abs(Year(endDate) - Year(startDate))
End Function</code></pre>
<h3 id="pattern-7-validate-year-range">Pattern 7: Validate Year Range</h3>
<pre><code class="language-vbnet">Function IsYearInRange(dt As Date, minYear As Integer, maxYear As Integer) As Boolean
Dim yr As Integer
yr = Year(dt)
IsYearInRange = (yr >= minYear And yr <= maxYear)
End Function</code></pre>
<h3 id="pattern-8-get-years-until-date">Pattern 8: Get Years Until Date</h3>
<pre><code class="language-vbnet">Function YearsUntil(targetDate As Date) As Integer
YearsUntil = Year(targetDate) - Year(Date)
End Function</code></pre>
<h3 id="pattern-9-add-years-to-date">Pattern 9: Add Years to Date</h3>
<pre><code class="language-vbnet">Function AddYears(dt As Date, years As Integer) As Date
AddYears = DateSerial(Year(dt) + years, Month(dt), Day(dt))
End Function</code></pre>
<h3 id="pattern-10-get-year-to-date-range">Pattern 10: Get Year-to-Date Range</h3>
<pre><code class="language-vbnet">Sub GetYTDRange(ByRef startDate As Date, ByRef endDate As Date)
startDate = DateSerial(Year(Date), 1, 1)
endDate = Date
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-age-calculator-class">Example 1: Age Calculator Class</h3>
<pre><code class="language-vbnet">' Class: AgeCalculator
' Calculates precise age with various options
Option Explicit
Public Function GetAge(birthDate As Date, Optional asOfDate As Variant) As Integer
Dim referenceDate As Date
Dim age As Integer
If IsMissing(asOfDate) Then
referenceDate = Date
Else
referenceDate = CDate(asOfDate)
End If
age = Year(referenceDate) - Year(birthDate)
' Adjust if birthday hasn't occurred yet
If Month(referenceDate) < Month(birthDate) Or _
(Month(referenceDate) = Month(birthDate) And _
Day(referenceDate) < Day(birthDate)) Then
age = age - 1
End If
GetAge = age
End Function
Public Function GetAgeInYearsAndMonths(birthDate As Date) As String
Dim years As Integer
Dim months As Integer
Dim tempDate As Date
years = GetAge(birthDate)
tempDate = DateSerial(Year(birthDate) + years, Month(birthDate), Day(birthDate))
months = DateDiff("m", tempDate, Date)
GetAgeInYearsAndMonths = years & " years, " & months & " months"
End Function
Public Function WillBeBirthdayThisYear(birthDate As Date) As Boolean
Dim birthdayThisYear As Date
birthdayThisYear = DateSerial(Year(Date), Month(birthDate), Day(birthDate))
WillBeBirthdayThisYear = (birthdayThisYear >= Date)
End Function
Public Function GetAgeAtDate(birthDate As Date, targetDate As Date) As Integer
GetAgeAtDate = GetAge(birthDate, targetDate)
End Function</code></pre>
<h3 id="example-2-fiscal-year-manager-module">Example 2: Fiscal Year Manager Module</h3>
<pre><code class="language-vbnet">' Module: FiscalYearManager
' Manages fiscal year calculations
Option Explicit
Private m_FiscalStartMonth As Integer
Public Sub SetFiscalYearStart(startMonth As Integer)
If startMonth < 1 Or startMonth > 12 Then
Err.Raise 5, , "Start month must be between 1 and 12"
End If
m_FiscalStartMonth = startMonth
End Sub
Public Function GetFiscalYear(dt As Date) As Integer
If m_FiscalStartMonth = 0 Then m_FiscalStartMonth = 1
If Month(dt) >= m_FiscalStartMonth Then
GetFiscalYear = Year(dt)
Else
GetFiscalYear = Year(dt) - 1
End If
End Function
Public Function GetFiscalYearStart(fiscalYear As Integer) As Date
If m_FiscalStartMonth = 0 Then m_FiscalStartMonth = 1
GetFiscalYearStart = DateSerial(fiscalYear, m_FiscalStartMonth, 1)
End Function
Public Function GetFiscalYearEnd(fiscalYear As Integer) As Date
Dim endMonth As Integer
Dim endYear As Integer
If m_FiscalStartMonth = 0 Then m_FiscalStartMonth = 1
endMonth = m_FiscalStartMonth - 1
If endMonth = 0 Then endMonth = 12
If m_FiscalStartMonth = 1 Then
endYear = fiscalYear
Else
endYear = fiscalYear + 1
End If
GetFiscalYearEnd = DateSerial(endYear, endMonth, Day(DateSerial(endYear, endMonth + 1, 0)))
End Function
Public Function FormatFiscalYear(fiscalYear As Integer) As String
If m_FiscalStartMonth = 1 Then
FormatFiscalYear = "FY" & fiscalYear
Else
FormatFiscalYear = "FY" & fiscalYear & "-" & Right$(CStr(fiscalYear + 1), 2)
End If
End Function</code></pre>
<h3 id="example-3-year-range-analyzer-class">Example 3: Year Range Analyzer Class</h3>
<pre><code class="language-vbnet">' Class: YearRangeAnalyzer
' Analyzes year ranges in date collections
Option Explicit
Public Function GetYearRange(dates() As Date) As String
Dim minYear As Integer
Dim maxYear As Integer
Dim i As Long
Dim yr As Integer
If UBound(dates) < LBound(dates) Then
GetYearRange = "No dates"
Exit Function
End If
minYear = Year(dates(LBound(dates)))
maxYear = minYear
For i = LBound(dates) To UBound(dates)
yr = Year(dates(i))
If yr < minYear Then minYear = yr
If yr > maxYear Then maxYear = yr
Next i
If minYear = maxYear Then
GetYearRange = CStr(minYear)
Else
GetYearRange = minYear & "-" & maxYear
End If
End Function
Public Function GetYearDistribution(dates() As Date) As Collection
Dim distribution As New Collection
Dim i As Long
Dim yr As Integer
Dim yearKey As String
Dim count As Long
For i = LBound(dates) To UBound(dates)
yr = Year(dates(i))
yearKey = CStr(yr)
On Error Resume Next
count = distribution(yearKey)
If Err.Number <> 0 Then
distribution.Add 1, yearKey
Err.Clear
Else
distribution.Remove yearKey
distribution.Add count + 1, yearKey
End If
On Error GoTo 0
Next i
Set GetYearDistribution = distribution
End Function
Public Function GetMostCommonYear(dates() As Date) As Integer
Dim distribution As Collection
Dim maxCount As Long
Dim maxYear As Integer
Dim yr As Variant
Set distribution = GetYearDistribution(dates)
maxCount = 0
For Each yr In distribution
If distribution(CStr(yr)) > maxCount Then
maxCount = distribution(CStr(yr))
maxYear = CInt(yr)
End If
Next yr
GetMostCommonYear = maxYear
End Function
Public Function GetUniqueYears(dates() As Date) As Collection
Dim years As New Collection
Dim i As Long
Dim yr As Integer
Dim yearKey As String
For i = LBound(dates) To UBound(dates)
yr = Year(dates(i))
yearKey = CStr(yr)
On Error Resume Next
years.Add yr, yearKey
On Error GoTo 0
Next i
Set GetUniqueYears = years
End Function</code></pre>
<h3 id="example-4-date-archive-organizer-module">Example 4: Date Archive Organizer Module</h3>
<pre><code class="language-vbnet">' Module: DateArchiveOrganizer
' Organizes files and data by year
Option Explicit
Public Function GetArchivePath(baseFolder As String, dt As Date) As String
Dim yearFolder As String
yearFolder = baseFolder
If Right$(yearFolder, 1) <> "\" Then yearFolder = yearFolder & "\"
yearFolder = yearFolder & Year(dt) & "\"
GetArchivePath = yearFolder
End Function
Public Function CreateYearlyArchiveFolders(baseFolder As String, _
startYear As Integer, _
endYear As Integer) As Long
Dim yr As Integer
Dim folderPath As String
Dim count As Long
count = 0
For yr = startYear To endYear
folderPath = baseFolder
If Right$(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
folderPath = folderPath & yr & "\"
On Error Resume Next
MkDir folderPath
If Err.Number = 0 Then count = count + 1
Err.Clear
On Error GoTo 0
Next yr
CreateYearlyArchiveFolders = count
End Function
Public Function GetYearFromFilename(filename As String) As Integer
Dim parts() As String
Dim part As Variant
Dim yr As Integer
parts = Split(filename, "_")
For Each part In parts
If IsNumeric(part) Then
yr = CInt(part)
If yr >= 1900 And yr <= 9999 Then
GetYearFromFilename = yr
Exit Function
End If
End If
Next part
GetYearFromFilename = 0
End Function
Public Function GenerateYearlyReport(data As Collection, reportYear As Integer) As String
Dim report As String
Dim item As Variant
Dim itemDate As Date
Dim count As Long
report = "Year " & reportYear & " Report" & vbCrLf
report = report & String$(50, "=") & vbCrLf
count = 0
For Each item In data
itemDate = CDate(item)
If Year(itemDate) = reportYear Then
count = count + 1
End If
Next item
report = report & "Total items: " & count & vbCrLf
GenerateYearlyReport = report
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The <code>Year</code> function can raise the following errors:
- <strong>Error 13 (Type mismatch)</strong>: If the argument cannot be interpreted as a date
- <strong>Error 5 (Invalid procedure call)</strong>: If the date is outside the valid range
- <strong>Returns Null</strong>: If the input is Null (not an error)</p>
<h2 id="performance-notes">Performance Notes</h2>
<ul>
<li>Very fast operation - direct extraction from date value</li>
<li>Constant time O(1) complexity</li>
<li>No performance penalty for different date formats</li>
<li>Safe to call repeatedly in loops</li>
<li>Consider caching if used extensively with same date</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Validate input</strong> before calling if date source is uncertain</li>
<li><strong>Handle Null</strong> explicitly when working with nullable date fields</li>
<li><strong>Use <code>DateSerial</code></strong> with Year for date construction/manipulation</li>
<li><strong>Combine with Month/Day</strong> for complete date component extraction</li>
<li><strong>Cache results</strong> when using same date repeatedly</li>
<li><strong>Use fiscal year functions</strong> for business date calculations</li>
<li><strong>Consider leap years</strong> when performing year-based calculations</li>
<li><strong>Use <code>DateDiff</code></strong> for accurate year differences accounting for partial years</li>
<li><strong>Test edge cases</strong> like end-of-year dates and leap year boundaries</li>
<li><strong>Document assumptions</strong> about calendar systems and year ranges</li>
</ol>
<h2 id="comparison-table">Comparison Table</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Returns</th>
<th>Range</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Year</code></td>
<td>Integer</td>
<td>100-9999</td>
<td>Year component</td>
</tr>
<tr>
<td><code>Month</code></td>
<td>Integer</td>
<td>1-12</td>
<td>Month component</td>
</tr>
<tr>
<td><code>Day</code></td>
<td>Integer</td>
<td>1-31</td>
<td>Day component</td>
</tr>
<tr>
<td><code>DatePart</code></td>
<td>Variant</td>
<td>Varies</td>
<td>Any date part</td>
</tr>
<tr>
<td><code>Format$</code></td>
<td>String</td>
<td>N/A</td>
<td>Formatted date</td>
</tr>
</tbody>
</table>
<h2 id="platform-notes">Platform Notes</h2>
<ul>
<li>Available in VB6, VBA, and <code>VBScript</code></li>
<li>Consistent behavior across platforms</li>
<li>Always returns four-digit year (not affected by regional settings)</li>
<li>Date range: January 1, 100 to December 31, 9999</li>
<li>Dates before year 100 or after year 9999 cause errors</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot return two-digit year (always four digits)</li>
<li>Cannot handle dates before year 100</li>
<li>Cannot handle dates after year 9999</li>
<li>No built-in fiscal year calculation (requires custom function)</li>
<li>Does not account for different calendar systems</li>
<li>No built-in leap year detection (requires separate function)</li>
<li>Cannot extract century separately (must calculate from year)</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>