Important alert: (current site time 4/18/2014 11:21:44 AM EDT)
 

VB icon

Get Last Day of Month Function

Email
Submitted on: 11/10/2002 8:27:11 AM
By: Johannf  
Level: Beginner
User Rating: By 2 Users
Compatibility: ASP (Active Server Pages)
Views: 22821
(About the author)
 
     Get Last Day of Month Function Pass the function any day and it will return the last day of the month of that date.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Get Last Day of Month Function
' Description:Get Last Day of Month Function
Pass the function any day and it will return the last day of the month of that date.
' By: Johannf
'
' Inputs:Date
'
' Returns:Integer
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7986&lngWId=4'for details.'**************************************

Function LastDayOfMonth(DateIn)
	Dim TempDate
	TempDate = Year(dateIn) & "-" & Month(DateIn) & "-"
	If IsDate(TempDate & "28") Then LastDayOfMonth = 28
	If IsDate(TempDate & "29") Then LastDayOfMonth = 29
	If IsDate(TempDate & "30") Then LastDayOfMonth = 30
	If IsDate(TempDate & "31") Then LastDayOfMonth = 31
End Function


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/12/2002 10:48:13 PM

Good~!!!
(If this comment was disrespectful, please report it.)

 
11/15/2002 7:43:27 AMAndrew Steitz

5 globes. Why? Simplicity, baby. I might add a check to see if the date passed in is valid before extracting the year and month, though. Always validate data before manipulating.
(If this comment was disrespectful, please report it.)

 
11/17/2002 11:52:48 PMShannon Harmon

Nice routine, you can also do it this way. If this doesn't get chopped to death as a comment.

<%
Response.Write LastDayOfMonth(Now())

Function LastDayOfMonth(ByVal dteDate)
Dim dteRet

If IsDate(dteDate) Then
dteRet = DateValue(Month(dteDate) & "/1/" & Year(dteDate))
dteRet = DateAdd("m", 1, dteRet) - 1
LastDayOfMonth = Day(dteRet)
Else
LastDayOfMonth = NULL
End If
End Function
%>
(If this comment was disrespectful, please report it.)

 
11/25/2002 9:46:23 AMRiverside

Or Like this:

Function ReturnMonthEnd(strDate)
mm = Month(strDate) + 1
If mm > 12 Then mm = 1
ReturnMonthEnd = DateValue(mm
(If this comment was disrespectful, please report it.)

 
3/12/2003 5:53:27 AM

wasting time to write the code...

how bout my code:

msgbox day(dateadd("d",-1, monthname(month(date)+1)&"/1/"&year(date)))


from the code above, no matter what your PC setting for date format wether dd/mm/yyyy or mm/dd/yyyy. the code will works...

wassalam
(If this comment was disrespectful, please report it.)

 
5/1/2003 11:36:58 AM

Another solution:
LastDay = DatePart("d",DateSerial(Year(dteDate),Month(dteDate)+1,0))
(If this comment was disrespectful, please report it.)

 
11/11/2003 11:08:38 PM

This is an awesome function. Not only does it work but it is easy on the eyes and brain.
(If this comment was disrespectful, please report it.)

 
11/13/2003 5:10:21 AM

One error is there in your coding,
You should put these coding lines in reverse order.
That's is start from 31 instead of 28 and go towards 28.
Got it.
(If this comment was disrespectful, please report it.)

 
12/15/2006 7:20:11 AMMuhammad Imran

Good Work
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.