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 % 4) == 0) { alert( y + " is a leap year."); }
else { alert( y + " is not a leap year.");}
</script>

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.

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.

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.