<!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 - monthname - Datetime">
<title>monthname - 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> / monthname</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="monthname-function">MonthName Function</h1>
<p>Returns a String indicating the specified month.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">MonthName(month, [abbreviate])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>month</strong> (Required) - Numeric designation of the month. For example, January is 1, February is 2, and so on.</li>
<li><strong>abbreviate</strong> (Optional) - Boolean value that indicates if the month name is to be abbreviated. If omitted, the default is False, which means the month name is not abbreviated.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <strong>String</strong> representing the month name:
- If abbreviate is False or omitted: Full month name (e.g., "January", "February")
- If abbreviate is True: Abbreviated month name (e.g., "Jan", "Feb")</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>MonthName</code> function returns the localized name of the month based on the system's regional settings.
It is commonly used for displaying month names in user interfaces, reports, and formatted output.</p>
<h3 id="key-characteristics">Key Characteristics:</h3>
<ul>
<li>Returns localized month names based on system regional settings</li>
<li>Month parameter must be between 1 and 12</li>
<li>Error 5 (Invalid procedure call) if month is less than 1 or greater than 12</li>
<li>Abbreviate parameter is optional; defaults to False (full name)</li>
<li>Abbreviated names are typically 3 characters but may vary by locale</li>
<li>Much simpler than maintaining arrays of month names</li>
<li>Can be combined with <code>Month()</code> function for date formatting</li>
<li>Respects system locale for internationalization</li>
</ul>
<h3 id="common-use-cases">Common Use Cases:</h3>
<ul>
<li>Display month names in user interfaces</li>
<li>Format dates for reports and output</li>
<li>Create drop-down lists of months</li>
<li>Generate calendar displays</li>
<li>Internationalized date formatting</li>
<li>Chart and graph labels</li>
<li>File naming with month names</li>
<li>Data export with readable month names</li>
</ul>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Display Month Names</strong> - Convert month numbers to readable names</li>
<li><strong>Report Headers</strong> - Format report titles with month names</li>
<li><strong>Calendar Controls</strong> - Populate month selectors</li>
<li><strong>Data Export</strong> - Include readable month names in exports</li>
<li><strong>User Interface</strong> - Display current or selected month</li>
<li><strong>Chart Labels</strong> - Label chart axes with month names</li>
<li><strong>File Naming</strong> - Create month-based file names</li>
<li><strong>Internationalization</strong> - Automatic locale-based month names</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<pre><code class="language-vbnet">' Example 1: Get full month name
Dim monthName As String
monthName = MonthName(3)
' Returns "March"</code></pre>
<pre><code class="language-vbnet">' Example 2: Get abbreviated month name
Dim shortMonth As String
shortMonth = MonthName(11, True)
' Returns "Nov"</code></pre>
<pre><code class="language-vbnet">' Example 3: Display current month
Dim currentMonthName As String
currentMonthName = MonthName(Month(Date))
MsgBox "Current month is " & currentMonthName</code></pre>
<pre><code class="language-vbnet">' Example 4: Format date with month name
Dim formattedDate As String
Dim d As Date
d = #5/15/2025#
formattedDate = MonthName(Month(d)) & " " & Day(d) & ", " & Year(d)
' Returns "May 15, 2025"</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<pre><code class="language-vbnet">' Pattern 1: Safe month name with validation
Function SafeMonthName(monthNumber As Integer, _
Optional abbreviate As Boolean = False) As String
If monthNumber < 1 Or monthNumber > 12 Then
SafeMonthName = ""
Else
SafeMonthName = MonthName(monthNumber, abbreviate)
End If
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 2: Populate month combo box
Sub PopulateMonthCombo(cbo As ComboBox, Optional abbreviate As Boolean = False)
Dim i As Integer
cbo.Clear
For i = 1 To 12
cbo.AddItem MonthName(i, abbreviate)
Next i
End Sub</code></pre>
<pre><code class="language-vbnet">' Pattern 3: Get month name from date
Function GetMonthNameFromDate(dateValue As Date, _
Optional abbreviate As Boolean = False) As String
GetMonthNameFromDate = MonthName(Month(dateValue), abbreviate)
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 4: Format date range with month names
Function FormatDateRange(startDate As Date, endDate As Date) As String
Dim startMonth As String
Dim endMonth As String
startMonth = MonthName(Month(startDate), True)
endMonth = MonthName(Month(endDate), True)
FormatDateRange = startMonth & " " & Day(startDate) & " - " & _
endMonth & " " & Day(endDate) & ", " & Year(endDate)
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 5: Create month-based filename
Function GetMonthlyFileName(fileDate As Date, fileType As String) As String
Dim monthAbbrev As String
monthAbbrev = MonthName(Month(fileDate), True)
GetMonthlyFileName = fileType & "_" & Year(fileDate) & "_" & monthAbbrev & ".dat"
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 6: Build month selection array
Function GetMonthArray(fullNames As Boolean) As String()
Dim months() As String
Dim i As Integer
ReDim months(1 To 12)
For i = 1 To 12
months(i) = MonthName(i, Not fullNames)
Next i
GetMonthArray = months
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 7: Format month/year string
Function FormatMonthYear(dateValue As Date, abbreviate As Boolean) As String
FormatMonthYear = MonthName(Month(dateValue), abbreviate) & " " & Year(dateValue)
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 8: Get fiscal month name
Function GetFiscalMonthName(fiscalMonth As Integer, abbreviate As Boolean) As String
' Fiscal month 1 might be October (calendar month 10)
Dim calendarMonth As Integer
Dim fiscalStartMonth As Integer
fiscalStartMonth = 10 ' October
calendarMonth = fiscalMonth + fiscalStartMonth - 1
If calendarMonth > 12 Then
calendarMonth = calendarMonth - 12
End If
GetFiscalMonthName = MonthName(calendarMonth, abbreviate)
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 9: Create report header
Function CreateMonthlyReportHeader(reportDate As Date) As String
Dim header As String
header = "Monthly Report - " & MonthName(Month(reportDate)) & " " & Year(reportDate)
CreateMonthlyReportHeader = header
End Function</code></pre>
<pre><code class="language-vbnet">' Pattern 10: Compare month names
Function IsSameMonth(date1 As Date, date2 As Date) As Boolean
IsSameMonth = (MonthName(Month(date1)) = MonthName(Month(date2))) And _
(Year(date1) = Year(date2))
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-calendar-generator">Example 1: Calendar Generator</h3>
<pre><code class="language-vbnet">' Class: CalendarGenerator
' Generates formatted calendar displays
Option Explicit
Private m_year As Integer
Private m_month As Integer
Private m_firstDayOfWeek As VbDayOfWeek
Public Sub Initialize(calYear As Integer, calMonth As Integer)
m_year = calYear
m_month = calMonth
m_firstDayOfWeek = vbSunday
End Sub
Public Function GetMonthHeader(abbreviate As Boolean) As String
GetMonthHeader = MonthName(m_month, abbreviate) & " " & m_year
End Function
Public Function GenerateCalendar() As String
Dim calendar As String
Dim firstDay As Date
Dim lastDay As Date
Dim currentDay As Date
Dim dayOfWeek As Integer
Dim weekRow As String
Dim dayCount As Integer
' Create header
calendar = String(28, " ") & GetMonthHeader(False) & vbCrLf
calendar = calendar & "Su Mo Tu We Th Fr Sa" & vbCrLf
' Get first and last day of month
firstDay = DateSerial(m_year, m_month, 1)
lastDay = DateSerial(m_year, m_month + 1, 0)
' Start with padding for first week
dayOfWeek = Weekday(firstDay, m_firstDayOfWeek)
weekRow = String((dayOfWeek - 1) * 4, " ")
' Fill in days
currentDay = firstDay
Do While currentDay <= lastDay
weekRow = weekRow & Format(Day(currentDay), "00") & " "
dayOfWeek = dayOfWeek + 1
If dayOfWeek > 7 Then
calendar = calendar & weekRow & vbCrLf
weekRow = ""
dayOfWeek = 1
End If
currentDay = DateAdd("d", 1, currentDay)
Loop
If Len(weekRow) > 0 Then
calendar = calendar & weekRow & vbCrLf
End If
GenerateCalendar = calendar
End Function
Public Function GetAllMonthsForYear(abbreviate As Boolean) As String()
Dim months() As String
Dim i As Integer
ReDim months(1 To 12)
For i = 1 To 12
months(i) = MonthName(i, abbreviate)
Next i
GetAllMonthsForYear = months
End Function</code></pre>
<h3 id="example-2-report-generator-with-month-names">Example 2: Report Generator with Month Names</h3>
<pre><code class="language-vbnet">' Class: MonthlyReportFormatter
' Formats reports with localized month names
Option Explicit
Private m_useAbbreviations As Boolean
Private m_reportData As Collection
Public Sub Initialize(useAbbrev As Boolean)
m_useAbbreviations = useAbbrev
Set m_reportData = New Collection
End Sub
Public Sub AddMonthData(monthNum As Integer, dataValue As Double)
Dim entry As String
entry = MonthName(monthNum, m_useAbbreviations) & ":" & dataValue
m_reportData.Add entry
End Sub
Public Function GenerateReport() As String
Dim report As String
Dim item As Variant
Dim i As Integer
report = "Monthly Summary Report" & vbCrLf
report = report & String(50, "-") & vbCrLf
For Each item In m_reportData
report = report & item & vbCrLf
Next item
GenerateReport = report
End Function
Public Function FormatDateWithMonth(dateValue As Date) As String
FormatDateWithMonth = MonthName(Month(dateValue), m_useAbbreviations) & _
" " & Day(dateValue) & ", " & Year(dateValue)
End Function
Public Function GetQuarterMonths(quarter As Integer) As String
Dim months As String
Dim startMonth As Integer
Dim i As Integer
startMonth = ((quarter - 1) * 3) + 1
For i = 0 To 2
If i > 0 Then months = months & ", "
months = months & MonthName(startMonth + i, m_useAbbreviations)
Next i
GetQuarterMonths = months
End Function</code></pre>
<h3 id="example-3-internationalized-date-formatter">Example 3: Internationalized Date Formatter</h3>
<pre><code class="language-vbnet">' Module: InternationalDateFormatter
' Provides locale-aware date formatting using MonthName
Option Explicit
Public Enum DateFormat
dfLong = 0 ' "January 15, 2025"
dfMedium = 1 ' "Jan 15, 2025"
dfShort = 2 ' "01/15/2025"
dfMonthYear = 3 ' "January 2025"
dfMonthYearShort = 4 ' "Jan 2025"
End Enum
Public Function FormatDate(dateValue As Date, formatType As DateFormat) As String
Select Case formatType
Case dfLong
FormatDate = MonthName(Month(dateValue)) & " " & _
Day(dateValue) & ", " & Year(dateValue)
Case dfMedium
FormatDate = MonthName(Month(dateValue), True) & " " & _
Day(dateValue) & ", " & Year(dateValue)
Case dfShort
FormatDate = Format(dateValue, "mm/dd/yyyy")
Case dfMonthYear
FormatDate = MonthName(Month(dateValue)) & " " & Year(dateValue)
Case dfMonthYearShort
FormatDate = MonthName(Month(dateValue), True) & " " & Year(dateValue)
End Select
End Function
Public Function FormatDateRange(startDate As Date, endDate As Date, _
abbreviate As Boolean) As String
Dim result As String
If Year(startDate) = Year(endDate) Then
If Month(startDate) = Month(endDate) Then
' Same month and year
result = MonthName(Month(startDate), abbreviate) & " " & _
Day(startDate) & "-" & Day(endDate) & ", " & Year(startDate)
Else
' Different months, same year
result = MonthName(Month(startDate), abbreviate) & " " & Day(startDate) & " - " & _
MonthName(Month(endDate), abbreviate) & " " & Day(endDate) & ", " & _
Year(endDate)
End If
Else
' Different years
result = MonthName(Month(startDate), abbreviate) & " " & Day(startDate) & ", " & _
Year(startDate) & " - " & _
MonthName(Month(endDate), abbreviate) & " " & Day(endDate) & ", " & _
Year(endDate)
End If
FormatDateRange = result
End Function
Public Function GetMonthNames(abbreviate As Boolean) As String()
Dim months(1 To 12) As String
Dim i As Integer
For i = 1 To 12
months(i) = MonthName(i, abbreviate)
Next i
GetMonthNames = months
End Function
Public Function ParseMonthName(monthString As String) As Integer
Dim i As Integer
' Try full names first
For i = 1 To 12
If UCase(MonthName(i)) = UCase(monthString) Then
ParseMonthName = i
Exit Function
End If
Next i
' Try abbreviated names
For i = 1 To 12
If UCase(MonthName(i, True)) = UCase(monthString) Then
ParseMonthName = i
Exit Function
End If
Next i
ParseMonthName = 0 ' Not found
End Function</code></pre>
<h3 id="example-4-chart-label-generator">Example 4: Chart Label Generator</h3>
<pre><code class="language-vbnet">' Class: ChartLabelGenerator
' Generates chart labels with month names
Option Explicit
Private m_startMonth As Integer
Private m_monthCount As Integer
Private m_abbreviate As Boolean
Public Sub Initialize(startMonth As Integer, monthCount As Integer, _
abbreviate As Boolean)
If startMonth < 1 Or startMonth > 12 Then
Err.Raise 5, "ChartLabelGenerator", "Invalid start month"
End If
m_startMonth = startMonth
m_monthCount = monthCount
m_abbreviate = abbreviate
End Sub
Public Function GetLabels() As String()
Dim labels() As String
Dim i As Integer
Dim currentMonth As Integer
ReDim labels(0 To m_monthCount - 1)
For i = 0 To m_monthCount - 1
currentMonth = m_startMonth + i
' Wrap around if exceeds 12
If currentMonth > 12 Then
currentMonth = currentMonth - 12
End If
labels(i) = MonthName(currentMonth, m_abbreviate)
Next i
GetLabels = labels
End Function
Public Function GetLabelWithYear(monthNum As Integer, yearNum As Integer) As String
GetLabelWithYear = MonthName(monthNum, m_abbreviate) & " '" & _
Right(CStr(yearNum), 2)
End Function
Public Function GetQuarterLabel(quarter As Integer, yearNum As Integer) As String
Dim firstMonth As Integer
firstMonth = ((quarter - 1) * 3) + 1
GetQuarterLabel = "Q" & quarter & " (" & _
MonthName(firstMonth, True) & "-" & _
MonthName(firstMonth + 2, True) & " " & _
yearNum & ")"
End Function
Public Function GetYearLabels(abbreviate As Boolean) As String()
Dim labels(1 To 12) As String
Dim i As Integer
For i = 1 To 12
labels(i) = MonthName(i, abbreviate)
Next i
GetYearLabels = labels
End Function
Public Function FormatMultiYearLabel(monthNum As Integer, _
startYear As Integer, _
endYear As Integer) As String
FormatMultiYearLabel = MonthName(monthNum, m_abbreviate) & " " & _
startYear & "-" & endYear
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">On Error Resume Next
monthName = MonthName(monthNum)
If Err.Number = 5 Then
MsgBox "Invalid month number. Must be between 1 and 12."
ElseIf Err.Number <> 0 Then
MsgBox "Error getting month name: " & Err.Description
End If
On Error GoTo 0</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>MonthName()</code> is very fast - simple lookup operation</li>
<li>For repeated calls, consider caching month name arrays</li>
<li>No performance difference between full and abbreviated names</li>
<li>Locale lookup may add minimal overhead but is negligible</li>
<li>More efficient than maintaining hardcoded month name arrays</li>
<li>Can be called millions of times without performance concerns</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Validate month numbers</strong> - Always check that month is 1-12 before calling</li>
<li><strong>Use for localization</strong> - Leverages system locale for international support</li>
<li><strong>Combine with <code>Month()</code></strong> - Extract month number from dates, then get name</li>
<li><strong>Consider abbreviations</strong> - Use abbreviated names for space-constrained displays</li>
<li><strong>Cache for UI</strong> - Populate drop-downs once, not on every refresh</li>
<li><strong>Document format</strong> - Clearly state whether full or abbreviated names are expected</li>
<li><strong>Test locales</strong> - Test with different regional settings for international apps</li>
<li><strong>Handle errors</strong> - Wrap in error handling for robust code</li>
<li><strong>Use constants</strong> - Define month number constants for readability</li>
<li><strong>Prefer over arrays</strong> - Use <code>MonthName()</code> instead of hardcoded name arrays</li>
</ol>
<h2 id="comparison-with-other-approaches">Comparison with Other Approaches</h2>
<table>
<thead>
<tr>
<th>Approach</th>
<th>Pros</th>
<th>Cons</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong><code>MonthName()</code></strong></td>
<td>Automatic localization, simple, no maintenance</td>
<td>Requires VBA/VB6, slightly slower than array</td>
</tr>
<tr>
<td><strong>Hardcoded array</strong></td>
<td>Fast, full control</td>
<td>No localization, maintenance burden</td>
</tr>
<tr>
<td><strong><code>Format()</code></strong></td>
<td>Flexible formatting</td>
<td>Returns different formats, not just name</td>
</tr>
<tr>
<td><strong><code>DatePart()</code></strong></td>
<td>Returns number, not name</td>
<td>Need to convert to name separately</td>
</tr>
</tbody>
</table>
<h2 id="localization-notes">Localization Notes</h2>
<p>The <code>MonthName</code> function returns month names based on the system's regional settings:
- <strong>English (US)</strong>: "January", "February", etc. / "Jan", "Feb", etc.
- <strong>Spanish</strong>: "enero", "febrero", etc. / "ene", "feb", etc.
- <strong>French</strong>: "janvier", "février", etc. / "janv", "févr", etc.
- <strong>German</strong>: "Januar", "Februar", etc. / "Jan", "Feb", etc.
- <strong>Japanese</strong>: "1月", "2月", etc.
- And many more locales...</p>
<h2 id="platform-notes">Platform Notes</h2>
<ul>
<li>Available in VBA (Excel, Access, Word, etc.)</li>
<li>Available in VB6</li>
<li><strong>Not available in <code>VBScript</code></strong> (<code>VBScript</code> lacks this function)</li>
<li>Returns String type</li>
<li>Consistent across all VBA platforms</li>
<li>Uses Control Panel regional settings</li>
<li>First introduced in VB6 and VBA 6.0</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Month parameter must be 1-12 (no 0 or 13+)</li>
<li>Error 5 if month is out of valid range</li>
<li>Returns names based on system locale only (cannot specify locale)</li>
<li>Abbreviated length varies by locale (usually 3 chars, but not always)</li>
<li>Not available in <code>VBScript</code></li>
<li>Cannot customize the returned names</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><strong>Month</strong> - Returns the month number (1-12) from a date</li>
<li><strong><code>WeekdayName</code></strong> - Returns the name of the weekday (similar function for days)</li>
<li><strong>Format</strong> - Can format dates with month names using format strings</li>
<li><strong><code>DatePart</code></strong> - Returns various parts of a date (including month number)</li>
<li><strong>Year</strong> - Returns the year component of a date</li>
<li><strong>Day</strong> - Returns the day component of a date</li>
</ul>
<h2 id="vb6-parser-notes">VB6 Parser Notes</h2>
<p><code>MonthName</code> is parsed as a regular function call (<code>CallExpression</code>). This module exists primarily
for documentation purposes to provide comprehensive reference material for VB6 developers
working with date formatting and display operations requiring localized month names.</p>
</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>