Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used the following function to get the Last Day, but not working properly.

JavaScript
function LastDayOfMonth(Year, Month)
 {
 return(new Date((new Date(Month+1 +"/"+ 1+"/"+ Year ))-1)).getDate();
 }


problem on this function is if we select last day of the month then the result will get as previous day.

suppose if we select Mar 30 then the result will show 31 (Correct), if we select Mar 31 then the result will get 30 only.
Posted
Updated 24-Aug-12 11:42am
v2

What about this:

C#
function getLastMonth(year, month)
{
        lastDateofTheMonth = new Date(year, month, 0)
        alert(lastDateofTheMonth);
}
 
Share this answer
 
Comments
Prasad_Kulkarni 24-Aug-12 6:26am    
+5
Manas Bhardwaj 24-Aug-12 7:34am    
thx!
Wendelius 24-Aug-12 17:43pm    
Looks... exactly what OP was asking for :)
Maciej Los 24-Aug-12 18:14pm    
Agree, my 5!
Try this:
JavaScript
function getDaysInMonth(aDate){
   // returns the last day of a given month
    var m = new Number(aDate.getMonth());
    var y = new Number(aDate.getYear());

    var tmpDate = new Date(y, m, 28);
    var checkMonth = tmpDate.getMonth();
    var lastDay = 27;

    while(lastDay <= 31){
        temp = tmpDate.setDate(lastDay + 1);
        if(checkMonth != tmpDate.getMonth())
            break;
        lastDay++
    }
    return lastDay;
}
 
Share this answer
 
Comments
Manas Bhardwaj 24-Aug-12 7:34am    
Correct +5!
Prasad_Kulkarni 24-Aug-12 7:39am    
Thank you Manas!
ridoy 24-Aug-12 8:03am    
good..+5
Prasad_Kulkarni 26-Aug-12 5:36am    
Thank you Ridoy!
Wendelius 24-Aug-12 17:43pm    
Yep, this should work just fine, my 5
Manas Bhardaway answer is very good!
I'll would like to show you another way to achieve this.

Java
function getLastMonth(year, month)
{
        lastDateofTheMonth = new Date(year, month +1, -1)
        alert(lastDateofTheMonth);
}


It should works too...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900