Top

Leap Year Check


Javascript Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  0.0 (of 0 scores)
Date Added  Nov 11, 2009
Last Updated  Nov 11, 2009
Tags  check  date  detect  javascript  leap  year 

Description

Just a simple script which uses maths to detect whether or not a year is a leap year or not. If the year inputted is then "true" will be returned, if not "false".

To use the script just use

leapYear(date);

e.g.

leapYear(2012);

This function can of course be used within any other function e.g.

alert(leapYear(2012));

Remember to put within <script type="text/javascript"></script> !!

Grab the Code

function leapYear()
{
	var date = new Date();
		date.setFullYear((arguments.length > 0) ? arguments[0] : date.getFullYear());
 
	var divNumber = ((date.getFullYear() + '').slice(-2) == "00") ? 400 : 4;
 
	return (date.getFullYear() / divNumber == Math.round(date.getFullYear() / divNumber)) ? true : false;
}

Comments

  (2)  RSS
MagicRevealer09
Comments: 20
 
Javascript Snippet:  Leap Year Check
Posted on Feb 14, 2010 8:32 am
Didn't work :S
Korvin
Comments: 421
 
Javascript Snippet:  Leap Year Check
Posted on Mar 4, 2010 12:59 pm
Code:
function leapCheck(y){y-=2008;if (y < 0){y*=-1;}if((y % 4) == 0){return true;}return false;}
that actually works how you want it to...

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  

Bottom