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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many days in a month?

Status
Not open for further replies.

ftc128

MIS
May 26, 2005
18
US
Does anybody have a VBScript routine they can share that calculates the number of days in a month accounting for leap years?

I wish there was something like "#days = Days(Month,Year)", but there ain't.

Thanks
 
you need to look at the DateSerial() function...

the below gives the last day of the month of today's date

Day(DateSerial( Year(date()) , Month(date()) + 1, 0 ))

-DNG
 
Here's a little something I wrote that should do what you're looking for with a quick spot of error checking thrown in...

Code:
<%
Function numDaysMonth(intMonth,intYear)
	
	intMonth	=	cint(intMonth)
	If intMonth > 0 and intMonth < 13 Then
		Select case intMonth
			Case 4,6,9,11
				intMaxDays	=	30
			Case 2
				If instr(1,(intYear / 4),".") > 0 Then
					intMaxDays	=	28
				Else
					intMaxDays	=	29
				End If
			Case else
				intMaxDays	=	31
		End Select
	Else
		intMaxDays	=	-1
	End If
	
	numDaysMonth	=	intMaxDays
End Function


daysInMonth	=	numDaysMonth(1,2005)
If daysInMonth	= -1 Then
	Response.Write "Invalid month"
Else
	Response.Write daysInMonth
End If
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top