Determining a leap year

Does anyone have a small snip of code that determines if a year is a leap year.

Or explain the rules that goven what determines if a year is a leap year.  

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It's very simple: It's a leap year if the year number is divisible by 4. The exception is that if the year is divisible by 100, it is _not_ a leap year. The exception to that is that the year 2000 _will_ be a leap year (so will the year 3000 if you plan to live that long).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I'm 99% sure--addmitidly not 100% sure that you missed one.  If the year is divisible by 400 it is a leap year (even though it is divisble by 100.)  Then If it is divisible by 2000, it is not a leap year (even though it is divisible by 400).  

Can anyone confirm that?
rock1Author Commented:
I did some more research on this.

Every 4 years is a leap year (1992, 1996, ...).  But, as an exception,
every 100 years is not a leap year (2100, 2200, ...).  But as an exception
to this exception, every 400 years is a leap year (2000, 2400, ...).

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Well we got three opinions now.  None completely agree with the other.  But does it matter?  Over what time span does this matter?  Most likely the only exception year that will matter is the year 2000.  If so, the question then is, is it a leap year.
rock1Author Commented:
Yes, 2000 is a leap year.  Because it is divisible by 400.


If the year is divisble by 4 it is a leap year.
But as an exception, every 100 years is not a leap year ( 2100, 2200 )
However, every 400 years is a leap year.  So 2000, 2400 are leap years.

Here is a code chunk I grabbed from someone.
bool bLeap = !( nYear % 4 ) && ( ( nYear % 100) || !( nYear % 400 ));

This is probably true. I am not sure whether year 3000 is really a leap year. But, as nietod said, it doesn't really matter for any practical purpose.
rock1Author Commented:
3000 Would not be a multiple because even though it can be divided by 4 and a 100, it is not a multiple of 400.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.