Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How do I determine if a given year is a leap year?

by Adam (Vicar)
on Sep 15, 2000 at 02:48 UTC ( #32583=categorized question: print w/replies, xml ) Need Help??
Contributed by Adam on Sep 15, 2000 at 02:48 UTC
Q&A  > dates and times

Answer: Leap Year
contributed by Adam

The leap year formula is:

A leap year is divisable by 4, but not by 100 (except if divisable by 400.)

if( 0 == $year % 4 and 0 != $year % 100 or 0 == $year % 400 ) { # Then $year is a leap year. }
Answer: Leap Year
contributed by Agyeya

The Date::Leapyear module will tell if a year is a (Gregorian) leap year:

use Date::Leapyear; if ( isleap(yyyy) ) { ... }
The function isleap(yyyy) returns 1 in a leap year, 0 otherwise.
Answer: Leap Year
contributed by mojotoad

DateTime offers an is_leap_year method for datetime objects.

They also provide leapsecond information via the DateTime::LeapSecond class, if you're into that.


Answer: Leap Year
contributed by TheHobbit

Well, may be... but your answer is false:).

Let's be precise: nowadays, your answer is right, but only since Gregorian reform, which has been take into account at different times in differents countries. Before that, and since 45BC, there was a leap year in every year divisible by 4 (NOTE: 45BC is year -44).

Even that isn't exatly true... At the beginning people did not understand what "once in 4 years" meant, and there was a period (between 45BC and 9BC) where there was a leap year every 3 years. Followed by a period (between 8BC and 8AD) where there was no leapyear at all.

See the Calendar FAQ .

Answer: Leap Year
contributed by Sol-Invictus

The exact dates when countries (that use the Western calendar) adopted the Gregorian version varies. See this section of the Calendar FAQ

Answer: How do I determine if a given year is a leap year?
contributed by DeadPoet

Not my work, but a great example:

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

The IsLeapYear subroutine is called with a year number, like


The function returns a true value if the year number is a leap year, false otherwise.

Get the correct number of days in February for the given year:

my $days_in_February = IsLeapYear($year) ? 29 : 28;

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2018-10-22 10:34 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (119 votes). Check out past polls.