It always happens : you have some calculation to do with a date and you forget to calculate those friggin' leap years. Here's how to do it cleanly.
var isLeap = theYear % 4 == 0Wait a minute, I forgot something! If it's not divisible by 100 but by 400 it is not a leap year… or maybe divisble by 100 and not by 400…
I can't remember that! Let's do it another way.
var isLeap = new Date(theYear,1,29).getDate() == 29;Nice! That's something simple that I enjoy. Let's dissect it.
new Date(theYear, 1, 29)This line simply create a date object that is initialized with the variable theYear. 1 is the month. As in java, the months are 0 = january and 11 = december (I hate that!). 29 is the last day of february when it's a leap year.
.getDate()Returns the date part of a date (sic!). It means that for May 23, 2007, it will return 23.