Sorcerer's Tower

JavaScript Leap Year check

I've just needed to fix a calendar that didn't implement leap years, and thus was missing 29th Feb this year.

Unfortunately, Google was bringing up various functions that rely on how browsers handle oddities in the built-in date functions, which isn't a sensible approach.

So, here is how to do it relying on the leap year formula:

function isLeapYear(year)
{return ((year%4 == 0) && (year%100 != 0 || year%400 == 0));}

And to implement that:

function readDaysInMonth(month,year)
	if (month == 1 && isLeapYear(year) == true) return 29;
	else return [31,28,31,30,31,30,31,31,30,31,30,31][month];

(Remember, in JS months are 0-indexed, hence 1 = Feb)