You are here: irt.org | FAQ | JavaScript | Date | Q1688 [ previous next ]
Simply hold the number of days in each month in two arrays, one for non-leap years, and another for leap years:
<script language="JavaScript"><!-- var daysofmonth = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); function LeapYear(year) { if ((year/4) != Math.floor(year/4)) return false; if ((year/100) != Math.floor(year/100)) return true; if ((year/400) != Math.floor(year/400)) return false; return true; } function y2k(number) { return (number < 1000) ? number + 1900 : number; } var today = new Date(); var year = y2k(today.getYear()); if (LeapYear(year) { daysofmonth = daysofmonthLY; } alert('February ' + year + ' has ' + daysofmonth[1] + ' days'); //--></script>