WebPusher.ie - web design ireland, Dublin based web site design company offering professional web design and related services - web development, database development and integration, web site copywriting, search engine optimisation

Home page of webpusher.ie web site design company - Dublin, Ireland

Articles for developers

Leap Year Correction

We all know that every four years (sort of) February has an extra day. This script is used to update an array of the days of each month so that any calculations, displays etc based on the arrays will be correct on Leap Years.

The array and the Javascript

The array we will be using is an array of the days of each month.

var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

and the function that will update the days in February. It looks for a year to be passed to it, but if none is given will use the current year instead.

function CheckLeapYear( theYear ) {
	if ( !theYear ) {
		var Now = new Date();
		theYear = Now.getYear();
		if ( theYear < 1000 ) theYear += 1900;
	}
	if ( theYear % 4 == 0 ) {
		if ( theYear % 100 == 0 && theYear % 400 == 0 ) MonthDays[1] = 29;
		if ( theYear % 100 != 0 ) MonthDays[1] = 29;
	} else {
		MonthDays[1] = 28;
	}
}

Whenever you need to access the MonthDays for a particular year simply call the function above and the MonthDays array will be updated accordingly.

web related news

Click here to validate XHTML with w3c standards Click here to validate XHTML with w3c standards