Calculating a Leap Year
In LiveCode

Back To LiveCode Main Page

For many calculating a leap year seems like a daunting task. It is really a simple three step process.

  1. Divide year by 4.
    If the remainder > 0 then the year is not a leap year.
    But if the remainder is 0 then go to step 2.

  2. Divide year by 100.
    If the remainder > 0 then the year is a leap year.
    But if the remainder is 0 then go to step 3

  3. Divide year by 400.
    If the remainder > 0 then the year is not a leap year.
    But if the remainder is 0 then the year is a leap year.

Now for the leap year function.

function isLeapYear yr
if yr mod 4 is 0 then # If the year divided by 4 has a remainder of zero, then run next test.
if yr mod 100 is 0 then # If the year divided by 100 has a remainder of zero, then run next test
if year mod 400 is 0 then # If year divided by 400 has a remainder of zero...
put 29 into NumOfDays # then it is a leap year but...
else # if the year divided by 400 has a remainder greater than zero then...
put 28 into NumOfDays #...it is not a leap year.
end if
else # If year divided by 100 has a remainder greater than zero...
put 29 into NumOfDays #...then it is a leap year.
end if
else # If year divided by 4 has a remainder greater than zero...
put 28 into NumOfDays #...then it is not a leap year.
end if
return numOfDays
end
isLeapYear


To use this function, all you have to do is call it with the year and the number of days for the month of February for the given month will be returned.