Month related date functions
This page shows the code for commonly used date functions and code fragments which involve a Month:
- First day in month
- Last day in month
- Day in month
- Month - Number
- Name of month - MonthName, optionally abbreviated
First day in month
The VBA fragment below calculates the first day in month for a given date.
Dim dtFirstDayInMonth As Date
Dim dt As Date: dt =
dtFirstDayInMonth = DateSerial(Year(dt), Month(dt), 1)
Last day in month
The VBA code below calculates the last day in month for a given date.
Dim dtLastDayInMonth As Date
Dim dt As Date: dt =
dtLastDayInMonth = DateSerial(Year(dt), Month(dt) + 1, 0)
Day in month
A whole number between 1 and 31, inclusive, representing the day of the month.
Dim iDayInMonth As Integer
Dim dt As Date: dt =
iDayInMonth = Day(dt)
Month - Number
Month
returns the number of the month of a given date, e.g for date #12/21/2022# it returns 12.
Dim dt As Date: dt = #12/21/2022#
Dim iMonth As Integer
iMonth = Month(dt)
Name of month - MonthName
Returns localized month name. On Eglish systems returns March
for the 3rd month.
Dim lngMonth As Long: lngMonth = 3
Dim strMonthName As String
strMonthName = MonthName(Month:=lngMonth)
Setting the optional argument Abbreviate:=True
will on Eglish systems return Feb
for the 2nd month.
Dim lngMonth As Long: lngMonth = 2
Dim strMonthName As String
strMonthName = MonthName(Month:=lngMonth, Abbreviate:=True)
Below image shows the Code VBA add-in support for VBA Date procedures.