Hi,
Title says it all.
Say I have the month as a number and a year.
Malcolm
Hi,
Title says it all.
Say I have the month as a number and a year.
Malcolm
function daysInMonth(Month, Year)
{
return 32 - new Date(Year, Month, 32).getDate();
}
daysInMonth(7,2009)
>>> 31
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
daysInMonth(7,2009); //31
daysInMonth(2,2009); //28
daysInMonth(2,2008); //29
The following takes any valid datetime value and returns the number of days in the associated month... it eliminates the ambiguity of both other answers...
// pass in any date as parameter anyDateInMonth
function daysInMonth(anyDateInMonth) {
return new Date(anyDateInMonth.getYear(),
++anyDateInMonth.getMonth(),
0).getDate();}
Date.prototype.monthDays= function(){
var d= new Date(this.getFullYear(), this.getMonth()+1, 0);
return d.getDate();
}
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
Have you tested the function for Feb 2012....it is a leap year and has 29 days...but the function shows 28 days...
Also u r welcome if you elaborate the New Date() and the format u r passing to it.....:)