Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Days in a month 1

Status
Not open for further replies.

JoeNg

Programmer
Apr 17, 2001
174
US
Is there a VB function when I can use to determine the # of days in a month, also handle leap year ie February?

Thanks
 
Thanks for your reply.

I was hoping for a simpler solution.
Oh well, one can always hope...
 
Apart from a native DaysInMonth function, how much simpler do you want it?
 
strongm,

In which Ver / Release do you have "DaysInMonth" as an intrinsic function?
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Save a step, and speed up the function, by removing the DateAdd function, and just use the Day and DateSerial functions....
 
You mean ...
Code:
Private Function LastDayInMonth(TheYear As Long, TheMonth As Long) As Long
LastDayInMonth = Day(DateSerial(TheYear, TheMonth + 1, 0))
End Function
 
MichaelRed: that's my point - there isn't a native function...

JohnYingling: :)
 
O.K., I get it (now).

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
<Grin>
Norris68: you know I meant that....

=Day(DateSerial(2002, iMonth + 1, 0))
 
CCLINT: I know ;)
Just putting it in black & white for MichaelRed ...
 
Putting it all together:

Public Function DaysInMonth(Optional ByVal vDate As Variant, _
Optional ByVal iMonth As Integer, Optional ByVal iYear As Integer) As Integer

If IsDate(vDate) Then
DaysInMonth= Day(DateSerial(Year(vDate), Month(vDate) + 1, 0))
Else
If iYear = 0 Then iYear = Year(Date)
If iMonth = 0 Then iMonth = Month(Date)

DaysInMonth= Day(DateSerial(iYear, iMonth + 1, 0))
End If
End Function

Everything is optional so if nothing is passed the current date will be used.
A date as a string or date variable can be passed, or just the month or just the year can be passed.
You could add error handling to determine if the if anything was passed in the vDate variable, and if so, but it's not a Date, then inform the user. The same with the iMonth if > than 12 or less than -12.
 
Thanks everyone for your replies.
I was hoping VB has a DaysInMonth function. I guess there isn't one.
Oh, well…
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top