Saturday, April 10, 2010

How to get numbers of days in a month

This post shows how to get the number of days in a month using jQuery by passing the month and year to a function.
The function is :
function getDaysInMonth(month, year) {

    return new Date(year, month, 0).getDate();
}


Example :
We want number of days from February 2009
alert(getDaysInMonth(2, 2009));

Output:

28


Why this works?

In fact here : alert(getDaysInMonth(2, 2009)); we ask for day 0 of March, cause months start from 0.But day 0 of March is last day of February, so with this trick we can get days from a custom month and year;

Labels: , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home