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

 on Sep 15, 2000 at 02:48 UTC Need Help??
Contributed by Adam on Sep 15, 2000 at 02:48 UTC
Q&A  > dates and times

 Answer: Leap Yearcontributed 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. } [download]``` Answer: Leap Yearcontributed by Agyeya The Date::Leapyear module will tell if a year is a (Gregorian) leap year: ``` use Date::Leapyear; if ( isleap(yyyy) ) { ... } [download]``` The function isleap(yyyy) returns 1 in a leap year, 0 otherwise. Answer: Leap Yearcontributed 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. Matt Answer: Leap Yearcontributed 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 . TheHobbit Answer: Leap Yearcontributed 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; } [download]``` The IsLeapYear subroutine is called with a year number, like ```IsLeapYear(2006); [download]``` 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; [download]```

• 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.
• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2018-06-22 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (120 votes). Check out past polls.

Notices?