Object.extend(Date.prototype, {
isLeap: function(){
var year = this.getFullYear();
return (year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0);
}
});
Refactorings
No refactoring yet !
macournoyer
September 21, 2007, September 21, 2007 07:30, permalink
I'm pretty sure the last year % 100 == 0 is not needed as we already test w/ a multiple of 100 (400), plus we now can remove parenthesis.
Object.extend(Date.prototype, {
isLeap: function(){
var year = this.getFullYear();
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
});
Gary Haran
September 21, 2007, September 21, 2007 08:05, permalink
Indeed we could remove one of the conditions. I replaced the condition with an integer value test instead, because JS automatically assumes that any value other than 0 is true. Unfortunately it means more parenthesis but it still makes the code shorter.
Can we find any other ways of making this shorter? :-D
PS: you should add a way to add more than one code snippet so you can show two pieces of code that work together. I'd love to show the test portion of this code in the same thread.
Object.extend(Date.prototype, {
isLeap: function(){
var year = this.getFullYear();
return !(year % 4) && !!(year % 100) || !(year % 400);
}
});
macournoyer
September 21, 2007, September 21, 2007 08:20, permalink
I'll try to add sections with ## like pastie, thx for the feedback Gary.
But that sure is a lot of !!(!negation != !false)
Gary Haran
September 21, 2007, September 21, 2007 21:23, permalink
The reason behind all those is purely to return a true boolean at the other end because that's what would be expected. One of the aims of any library code I write is to have as short a footprint as possible. Because I unit test functions like these I don't mind losing a bit of readability. Btw in the prototype source code uses the !! trick a few times to convert undefined to a boolean value.
macournoyer
September 23, 2007, September 23, 2007 13:13, permalink
Nice tip Gary! I didn't know about this idom!
And about sections, how about that:
def code puts 'cool' end
<h1><%= @title %></h1>
var Page = {
omg: function() {
alert('img');
}
};
br
September 28, 2007, September 28, 2007 00:45, permalink
function isLeap(y){return new Date(y, 2, 0).getDate()==29;}
Ali Karbassi
October 2, 2007, October 02, 2007 19:03, permalink
Going of what "br" said and other people's code, we could do this the following.
Object.extend(Date.prototype, {
isLeap: function()
{
return new Date(this.getFullYear(), 2, 0).getDate() == 29;
}
});
Owen
October 7, 2007, October 07, 2007 19:07, permalink
Since all leap years are multiples of 4, you don't need anything but %4. Right? Seems overly complicated to me.
Object.extend(Date.prototype, {
isLeap: function(){
return !(this.getFullYear() % 4);
}
});
Ali Karbassi
October 13, 2007, October 13, 2007 19:08, permalink
Owen:
According the Wikipedia, it's not JUST every 4 years.
if year modulo 400 is 0 then leap
else if year modulo 100 is 0 then no_leap
else if year modulo 4 is 0 then leap
else no_leap
sameer
October 24, 2007, October 24, 2007 05:14, permalink
wikkipedia code has some flaw in it.the years 100,200,300 are leap years,but according to the code output these are not leap years.an anyone explain that?
Mark Giblin
November 15, 2011, November 15, 2011 12:52, permalink
You can forget all these other methods that have flaws in them...
Set a date object to 29th Feb in the year to be tested and if the month is not == 2 then its not a leap year.
Job done.
This can easily be prototyped.
Enjoy.
function isLeapYear(){
dateObj = new Date;
var year = dateObj.getFullYear();
return new Date( year , 2, 29 ).getMonth()==2;
}
Mark Giblin
November 15, 2011, November 15, 2011 12:53, permalink
You can forget all these other methods that have flaws in them...
Set a date object to 29th Feb in the year to be tested and if the month is not == 2 then its not a leap year.
Job done.
This can easily be prototyped.
Enjoy.
function isLeapYear(){
dateObj = new Date;
var year = dateObj.getFullYear();
return new Date( year , 2, 29 ).getMonth()==2;
}
I'm pretty sure we can figure out a shorter and more elegant way to do this :-D