manolakshman said:
How do you readily come to know by a function the number of days in a
month.
It can be done by DateDiff(), but that seems to be more complicated.
Because of leap years, this can't really be determined without considering
the year. Here's a function that accepts a date, a month, or a month and
year, and returns the number of days in the month:
'----- start of code -----
Function fncDaysInMonth(pDateOrMonth As Variant, Optional pYear As Variant)
As Variant
' Given a date, or month and year, return the number of days in the
month.
' If month is given but year is not, then the current year is used.
'
' Usage examples:
' ?fncDaysInMonth(#12/1/2008#)
' 31
' ?fncDaysInMonth(6)
' 30
' ?fncDaysInMonth(2, 2007)
' 28
' ?fncDaysInMonth(2, 2008)
' 29
'
' Written by: Dirk Goldgar on: 21 August, 2008
Dim dtDate As Date
Dim intYear As Integer
If IsDate(pDateOrMonth) Then
dtDate = pDateOrMonth
fncDaysInMonth = Day(DateSerial(Year(dtDate), Month(dtDate) + 1, 0))
Else
If IsNull(pDateOrMonth) Then
fncDaysInMonth = Null
Else
If IsMissing(pYear) Or IsNull(pYear) Then
intYear = Year(Date)
Else
intYear = pYear
End If
fncDaysInMonth = Day(DateSerial(intYear, pDateOrMonth + 1, 0))
End If
End If
End Function
'----- end of code -----