Current time: 02-05-2012, 05:33 PM Hello There, Guest! (LoginRegister)


Thread Closed 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] The number of days in a month
04-28-2010, 03:27 PM
Post: #1
[SOLVED] The number of days in a month
As Time Toady said
Quote:There's more than one way to do it

I 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.

Bombastic and verbose but fast
Code:
Date.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 year
Date.prototype.getDaysInMonth.R = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// durations of months for the leap year
Date.prototype.getDaysInMonth.L = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

Shorter and longer
Code:
Date.prototype.getDaysInMonth = function ()
{
    var here = new Date(this.getTime());
    here.setDate(32);
    return 32 - here.getDate();
};

Laconic but long almost as previous
Code:
Date.prototype.getDaysInMonth = function()
{
    return (new Date(this.getFullYear(), this.getMonth() + 1, 0)).getDate();
};
Find all posts by this user
Thread Closed 


Forum Jump:


Forum Permissions
You cannot post new threads.
You cannot post replies.
You cannot post attachments.
HTML is turned off.
MyCode is turned on.
Smilies are turned on.
[img] is turned on.