Page 1 of 1

The number of days in a month

Posted: Wed Apr 28, 2010 5:16 am
by with-love-from-siberia
As Time Toady saidThere's more than one way to do itI think that all of you know how to know the number of days in a month. Let me collect all known (at least for me) methods to populate the subject. Bombastic and verbose but fastDate.prototype.isLeapYear = function(){ var y = this.getFullYear(); return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;};Date.prototype.getDaysInMonth = function(){ return arguments.callee[this.isLeapYear() ? 'L' : 'R'][this.getMonth()];};// durations of months for the regular yearDate.prototype.getDaysInMonth.R = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];// durations of months for the leap yearDate.prototype.getDaysInMonth.L = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];Shorter and longerDate.prototype.getDaysInMonth = function (){ var here = new Date(this.getTime()); here.setDate(32); return 32 - here.getDate();};Laconic but long almost as previousDate.prototype.getDaysInMonth = function(){ return (new Date(this.getFullYear(), this.getMonth() + 1, 0)).getDate();};