The number of days in a month
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- with-love-from-siberia
- Posts: 1
- Last visit: Wed Apr 28, 2010 5:16 am
The number of days in a month
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();};