Leap year: Difference between revisions

From Rosetta Code
Content added Content deleted
(JavaScript, ActionScript, PHP)
Line 47: Line 47:
return year % 400 == 0
return year % 400 == 0
return year % 4 == 0
return year % 4 == 0
</lang>

Asking for forgiveness instead of permission:

<lang python>import datetime

def is_leap_year(year):
try:
date(year, 2, 29)
except ValueError:
return False
return True
</lang>
</lang>

Revision as of 19:17, 20 July 2010

Task
Leap year
You are encouraged to solve this task according to the task description, using any language you may know.

Determine whether a given year is a leap year.

See Also

ActionScript

<lang actionscript>public function isLeapYear(year:int):Boolean {

   if (year % 100 == 0) {
       return (year % 400 == 0);
   }
   return (year % 4 == 0);

}</lang>

Java

<lang java>public static boolean isLeapYear(int year){

   if(year % 100 == 0) return year % 400 == 0;
   return year % 4 == 0;

}</lang>

JavaScript

<lang javascript>function isLeapYear(year) {

   if (year % 100 == 0) {
       return (year % 400 == 0);
   }
   return (year % 4 == 0);

}</lang>

PHP

<lang php><?php function isLeapYear(year) {

   if (year % 100 == 0) {
       return (year % 400 == 0);
   }
   return (year % 4 == 0);

}</lang>

With date('L'):

<lang php><?php function isLeapYear(year) {

   return (date('L', mktime(0, 0, 0, 2, 1, year)) === '1')

}</lang>

Python

<lang python>def is_leap_year(year):

   if year % 100 == 0:
       return year % 400 == 0
   return year % 4 == 0

</lang>

Asking for forgiveness instead of permission:

<lang python>import datetime

def is_leap_year(year):

   try:
       date(year, 2, 29)
   except ValueError:
       return False
   return True

</lang>