Method Date.getDaysInMonth
Return the number of days in the month.
-
Syntax
Number Date.getDaysInMonth()
Use this function to get the number of days in the month. - Return value The number of days in the month.
-
Examples
- To get the number of days in February 2008 (a leap year)
// Create date var d = new Date(2008,1,1); // Output is 29 d.getDaysInMonth();
-
Resources
An alternative algorithm is shown below. It is marginally slower because of the modulus computations.
Date.prototype.getDaysInMonth =
function(utc) {
var m = utc ? this.getUTCMonth() : this.getMonth();
if (m == 1)
return this.isLeapYear() ? 29 : 28;
// If m % 2 == 0 then if m < 8 then 31 else 30
// If m % 2 != 0 then if m > 6 then 31 else 30
return m % 2 ? m > 6 ? 31 : 30 : m < 8 ? 31 : 30;
};
-
Download
Source
Unless otherwise noted all code in the JSLab Standard Library is licensed as GPLv3. See http://www.gnu.org/licenses/gpl.html for the entire license.
This function has dependencies and will not work stand-alone. Use the download button above instead of copy/paste.
