Leap Year javascript check

I am using the following code for a counter and am having trouble getting it to check for leap years in the code. Can someone please look at this and tell me where I am going wrong?

function lbls_init()
{
	var sd = new Date(lbls_start);
	var dn = new Date();
	var nm = Math.floor((dn.getTime()-sd.getTime())/1000);

	_data['sec'] = nm%60;
	nm = Math.floor(nm/60);

	_data['min'] = nm%60;
	nm = Math.floor(nm/60);

	_data['hour'] = nm%24;
	nm = Math.floor(nm/24);

	if (nm >= 365)
	{
		_data['day'] = nm-365*Math.floor(nm/365);
	}
	else
	{
		_data['day'] = nm;
	}

	nm = Math.floor(nm/365);
	_data['year'] = nm;
	lbls_timer();
}

If the two digits in the end of a number is divisible by 4, the number is a leap year.
If the two digits in the end of a number is not divisible by 4, the number is not a leap year.

2010
The two digits in the end of a number is 10
10 is not divisible by 4.
2010 is not a leap year.

2012
The two digits in the end of a number is 12
12 is divisible by 4.
2012 is a leap year.

substr() Extracts the characters from a string, beginning at a specified start position, and through the specified number of character


<script type="text/javascript">

var y = prompt("enter a year","");

var s = y.substr(2); // the two digits in the end of a number

// alert(s); 

var n = Number(s);

if( (n &#37; 4) == 0) { alert( y + " is a leap year."); }

else { alert( y + " is not a leap year.");}


</script>

Ok, so how do I incorporate that into the code above?

You don’t have to take the last two numbers, you can just divide the whole year by 4 (since 100 is divisible by 4).

The simplest way to check for leap years is to let JavaScript do it for you.

var y = prompt("enter a year","");
var leapdate = new Date(y, 2, 0);
if (leapdate.date == 29) alert(y+' is a leap year');
else alert(y+' is not a leap year');

It simply checks if the day before the 1st March of the specified year is the 29th and if so then it must be a leap year. As long as the correct leap year rules are built into JavaScript you don’t even need to know those rules in order to use it.

Per http://timeanddate.com:

  1. Every year that is evenly divisible by four is a leap year;
  2. of those years, if it can be evenly divided by 100, it is NOT a leap year, unless
  3. the year is evenly divisible by 400. Then it is a leap year.
  1. There’s an extra leap year needed every 3300 years. Since the current calendar started in 1582 the first of these extras will presumably occur in 4882 and the following one on 8182 but I guess none of us wuill be around to worry about those leap years.

You don’t need to worry about leap year rules though if you let the programming language you are using use the built in code that already knows the part of the rules that is relevant.

The simplest rule to use with JavaScript is the one that says that if the day before the 1st March is the 29th then it’s a leap year. I posted the code that does the test that way earlier in this thread.

Date.isLeapYear= function(y){
	if(typeof y!= 'number') y= new Date().getFullYear();
	return new Date(y, 1, 29).getDate()== 29;
}

// test
Date.isLeapYear() // uses current year
Date.isLeapYear(1900) // returns false
Date.isLeapYear(2012) // returns true
Date.isLeapYear(2000) // returns true

Excellent, you can’t get anything simpler or more flexible than your implementation.

  1. Every year that is evenly divisible by four is a leap year;
  1. of those years, if it can be evenly divided by 100, it is NOT a leap year, unless
  2. the year is evenly divisible by 400. Then it is a leap year.

Interesting, glad to have learned that. Also I agree with felgall, mrhoo’s function is pretty nice.

While not wanting to bring planes down with my evil bug, just dividing by 4 is safe between 1901 and 2099. Having said that, Mr Hoo’s method is so short and neat, you might as well be safe.

Before 4882 JavaScript should have been updated to recognise that year as being a leap year as well and so letting JavaScript work it out will continue to work forever instead of needing updating to take into account one less leap year every 400 years and one more every 3300 years if you didn’t include them in your calculation.