Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryTutorials and Answers

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

How to Calculate Leap Years

Some software must know if the current or other specific year is a leap year. Not just calendar software, but any that requires knowing how many days are in a year or how many days are in a particular month of February.

This tutorial presents steps to do the calculations manually. Following that is code for doing it with PHP, JavaScript, and Perl.

There are 3 rules for doing it manually. The rules need to be done in order.

The first rule is to determine if the year number is evenly divisible by 4.

If there is a remainder when dividing the year by 4, then it is not a leap year and the other two rules are ignored. But if it is evenly divisible by 4, then it is a leap year only if it passes the following.

The second rule is to determine if the year number is evenly divisible by 100.

If there is a remainder when dividing the year by 100, then it is a leap year and the next rule is ignored. But if it is evenly divisible by 100, then it is not a leap year unless the next rule says it is.

The third rule is to determine if the year number is evenly divisible by 400.

If there is a remainder when dividing the year by 400, then it is not a leap year. But if it is evenly divisible by 400, then, yes, it is a leap year.

With those rules, code can be written with pretty much any computer programming language to determine whether or not a specific year number is a leap year. Code for PHP, JavaScript, and Perl are further below.

For the three represented programming languages, each has a function coded that is named IsLeapYear and returns either true (or the digit 1) or false (or the digit 0). Each step of the function might return true or false. If a step does not return, then the next step is consulted.

This is the logic implemented in the functions:

  1. If there is a remainder when dividing the year by 4, then it is not a leap year. The function returns false;

    But if there is no remainder, it might be a leap year and the next step runs.

  2. If there is a remainder when dividing the possible leap year by 100, then it is a leap year. The function returns true;

    But if there is no remainder, it might still be a leap year and the next step runs.

  3. If there is a remainder when dividing the likely leap year by 400, then it is not a leap year. The function returns false;

    But if there is no remainder, it is a leap year and the next step runs.

  4. The function returns true because it is indeed a leap year.

The above is fairly straight-forward for logic applicable to all programming languages represented in this article.

I've seen one-line leap year code, and some that use language built-in function names. But I wanted a logic that can be implemented with pretty much any programming language.

Determining Leap Year with PHP

Here is the PHP-coded version of the IsLeapYear function.

function IsLeapYear($year)
{
   if( $year % 4 ) { return false; }
   if( $year % 100 ) { return true; }
   if( $year % 400 ) { return false; }
   return true;
}

Example of use, assigning the number of days in February:

$February = IsLeapYear(2024) ? 29 : 28;

Determining Leap Year with JavaScript

Here is the JavaScript-coded version of the IsLeapYear function.

function IsLeapYear(year)
{
   if( year % 4 ) { return false; }
   if( year % 100 ) { return true; }
   if( year % 400 ) { return false; }
   return true;
}

Example of use, assigning the number of days in February:

var February = IsLeapYear(2024) ? 29 : 28;

Determining Leap Year with Perl

Here is the Perl-coded version of the IsLeapYear function (or subroutine, as it is called in Perl).

sub IsLeapYear
{
   my $year = shift;
   return 0 if $year % 4;
   return 1 if $year % 100;
   return 0 if $year % 400;
   return 1;
}

Example of use, assigning the number of days in February:

my $February = IsLeapYear(2024) ? 29 : 28;

When you implement one of the above in your own software, here is a list of the current and next 20 years you can use to compare the output of your implementation. The leap years are marked with a √ check mark.

2024 √

2025

2026

2027

2028 √

2029

2030

2031

2032 √

2033

2034

2035

2036 √

2037

2038

2039

2040 √

2041

2042

2043

2044 √

The same logic as used with the three represented programming languages is likely to be possible for other programming languages.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC