get the last day of any given month

# Thread: get the last day of any given month

1. Senior Member
Join Date
Dec 1969
Posts
330

## get the last day of any given month

Is there a function or an easy way to get the last day of any given month?

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## Use a calendar

Or code it manually... there isn&#039;t a built in function.<BR><BR>You&#039;ll want to look at WeekDay() and DateAdd() functions.

3. Senior Member
Join Date
Dec 1969
Posts
500

## Function

Try something like this:<BR><BR>Dim Max_Days(12)<BR><BR>For m = 1 to 12<BR>&nbsp;&nbsp;For d = 1 to 32<BR>&nbsp;&nbsp;&nbsp;&nbsp;Temp_Date = m & "/" & d & "/2001"<BR>&nbsp;&nbsp;&nbsp;&nbsp;If Not IsDate(Temp_Date) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iMax = d - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = 33 &#039;break out of loop<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>&nbsp;&nbsp;Next<BR>&nbsp;&nbsp;Max_Days(m) = iMax<BR>Next<BR><BR>NOTE: this is untested.

4. Senior Member
Join Date
Dec 1969
Posts
500

## Revised

Dim Max_Days(12) <BR><BR>For m = 1 to 12 <BR> For d = 28 to 32 <BR> Temp_Date = m & "/" & d & "/2001" <BR> If Not IsDate(Temp_Date) Then <BR> iMax = d - 1 <BR> d = 33 &#039;break out of loop <BR> End If <BR> Next <BR> Max_Days(m) = iMax <BR>Next <BR>

5. Senior Member
Join Date
Dec 1969
Posts
11,334

## Why are you going through all that trouble?

You know the max days in every month except Feb, and according to your function, it&#039;s going to be 28 this year anyways.<BR><BR>Why use 48 iterations when you can do it in one simple array?<BR><BR>MaxDays(0) = 31<BR>MaxDays(2) = 31<BR><BR>Besides, I think he&#039;s looking for the last day (Monday, Tuesday) of the month, something that can be accomplished in 2 or 3 lines.

6. Senior Member
Join Date
Dec 1969
Posts
500

## Oh, Gosh

I am thinking too hard.

7. Senior Member
Join Date
Dec 1969
Posts
1,424

## This should do the trick...

Check out some of the examples on this page...<BR><BR> http://www.flws.com.au/showusyourcode/codeLib/code/anyDate.asp?catID=1

8. Junior Member
Join Date
Dec 1969
Posts
21

## Leap Year Accomodation ???

I once forgot to test whether the Year was a Leap or not, altering the quantity of days in February (01 in a 00 based world):<BR><BR>if (todayMonth == 1)<BR> {<BR> if (((todayYear % 4 == 0) && (todayYear % 100 != 0)) &#124&#124 (todayYear % 400 == 0))<BR> {<BR> Amounts[todayMonth] = (Amounts[todayMonth] + 1)<BR> }<BR> }<BR><BR>.rob adams<BR>

9. Senior Member
Join Date
Dec 1969
Posts
896

## Super ez

where y = year, and m = month of date you want days in month of.<BR><BR>daysinmonth = dateadd("d", -1, dateserial(y, m + 1, 1))<BR>

10. Senior Member
Join Date
Dec 1969
Posts
2,493

## RE: must be bored

&#060;%<BR>If IsNumeric(request("year")) Then<BR> Dim searchDate<BR> Dim theday<BR> searchDate = request("month")+1 & "/1/" & request("year")<BR> theday = Left(formatdatetime(DateAdd("d",dateValue(searchDa te),-1),1),Instr(formatdatetime(DateAdd("d",dateValue(s earchDate),-1),1),",")-1)<BR>End If<BR>%&#062;<BR>&#060;form action="thispage.asp" method="post"&#062;<BR>&#060;Table&#062;<BR>&#060; tr&#062;<BR>&#060;td align="right"&#062;&#060;font face="arial" size="2"&#062;What month : &#060;/font&#062;&#060;/td&#062;<BR>&#060;td&#062;<BR>&#060;select name="month"&#062;<BR>&#060;option value=1 &#060;%If Request("month")=1 Then response.write " Selected"%&#062;&#062;January<BR>&#060;option value=2 &#060;%If Request("month")=2 Then response.write " Selected"%&#062;&#062;Febuary<BR>&#060;option value=3 &#060;%If Request("month")=3 Then response.write " Selected"%&#062;&#062;March<BR>&#060;option value=4 &#060;%If Request("month")=4 Then response.write " Selected"%&#062;&#062;April<BR>&#060;option value=5 &#060;%If Request("month")=5 Then response.write " Selected"%&#062;&#062;May<BR>&#060;option value=6 &#060;%If Request("month")=6 Then response.write " Selected"%&#062;&#062;June<BR>&#060;option value=7 &#060;%If Request("month")=7 Then response.write " Selected"%&#062;&#062;July<BR>&#060;option value=8 &#060;%If Request("month")=8 Then response.write " Selected"%&#062;&#062;August<BR>&#060;option value=9 &#060;%If Request("month")=9 Then response.write " Selected"%&#062;&#062;September<BR>&#060;option value=10 &#060;%If Request("month")=9 Then response.write " Selected"%&#062;&#062;October<BR>&#060;option value=11 &#060;%If Request("month")=11 Then response.write " Selected"%&#062;&#062;November<BR>&#060;option value=12 &#060;%If Request("month")=12 Then response.write " Selected"%&#062;&#062;December<BR>&#060;/Select&#062;<BR>&#060;/td&#062;<BR>&#060;/tr&#062;<BR>&#060;tr&#062;<BR>&#060;td align="right"&#062;&#060;font face="arial" size="2"&#062;What year : &#060;/font&#062;&#060;/td&#062;<BR>&#060;td&#062;&#060;input type="text" name="Year" size=4 maxlength=4 value="&#060;%=Request("year")%&#062;"&#062;&#060;/td&#062;<BR>&#060;/tr&#062;<BR>&#060;tr&#062;<BR>&#060;td align="right"&#062;&#060;font face="arial" size="2"&#062;The day will be : &#060;/font&#062;&#060;/td&#062;<BR>&#060;td&#062;&#060;input type="text" name="day" value="&#060;%=theday%&#062;"&#062;&#060;/td&#062;<BR>&#060;/tr&#062;<BR>&#060;tr&#062;<BR>&#060;td&#062;&nbsp; &#060;/td&#062;<BR>&#060;td&#062;&#060;input type="submit" value="Get Last Day"&#062;&#060;/td&#062;<BR>&#060;/tr&#062;<BR>&#060;/table&#062;<BR>&#060;/form&#062;<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•