days in month

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi
I have a question in visual basic
which function should I use to retrive the number of days in a specific month
thank you for your help
Tamar.
 
Here's a function that will do this (you send the "number" of the month and
the "year" to the function):



Public Function DaysInAMonth(ByVal intMonthNumber As Integer, ByVal
intYearValue As Integer) As Integer
If intMonthNumber < 0 Or intMonthNumber > 12 Then
DaysInAMonth = 0
Else
If intMonthNumber = 12 Then intMonthNumber = 0
DaysInAMonth = Day(DateSerial(intYearValue, intMonthNumber + 1, 0))
End If
End Function


To use this function, put it in a regular module (the module must be named
something other than the name of the function) and then you can call this
function this way to get the number of days in January 2004:

DaysMonth = DaysInAMonth(1, 2004)
 
Ken said:
Here's a function that will do this (you send the "number" of the month and
the "year" to the function):



Public Function DaysInAMonth(ByVal intMonthNumber As Integer, ByVal
intYearValue As Integer) As Integer
If intMonthNumber < 0 Or intMonthNumber > 12 Then
DaysInAMonth = 0
Else
If intMonthNumber = 12 Then intMonthNumber = 0
DaysInAMonth = Day(DateSerial(intYearValue, intMonthNumber + 1, 0))
End If
End Function


Just a FYI, there is no need to check if intMonthNumber=12

DateSerial has no problem understanding any combination of
numbers for year, month and day that result in a date in the
range 1/1/100 through 12/31/9999
 
Marshall Barton said:
Just a FYI, there is no need to check if intMonthNumber=12

DateSerial has no problem understanding any combination of
numbers for year, month and day that result in a date in the
range 1/1/100 through 12/31/9999

Now that you post this, I do recall that it works ok with 13 as the month
number ... thanks for the reminder!
 
Back
Top