The method to finding out if a year is a leap year uses the following steps.
If you can evenly divide a year by 4, then it IS a leap year, except..........
when it can be divided by 100, then it is a NOT a leap year. Unless........
if the year can be evenly divided by 400 then it IS a leap year.
Below is a flow chart to visually demonstrate this method.
So the the years 1600, 2000 and 2400 (evenly divisible by 400) are leap years but 1700, 1800, 1900, 2100 (are evenly divisible by 4 and 100 but are NOT evenly divisible by 400) are not leap years. All other years like 2004, 2008, 2012, 2016, 2020, 2024, etc. (That are not evenly divisible by 100 but are evenly divisible by 4) are leap years.
Reason why for this algorithm
According to Wikipedia, the earth orbits the sun approximately every 365.256363004 (Wikipedia link) days each year. So every four years, another day is added to try to correct the calendar. But 365.256363004 days is a bit more than the 365.25 days needed for it to work out even. So in a hundred years we will have to skip a leap year to compensate for the first adjustment. This is still not close enough; so every four hundred years we do allow a leap year. This is then considered close enough. I guess somewhere in the far distant future they may make further adjustments, but I won't be here to see them.
More Reading regarding Leap Years
Wikipedia: Leap Year
Wikipedia: Earth's Orbit
Nasa: Calendar Calculations
Gambas Leap Year Function Examples
For determining a leap year in a computer program written in the Gambas language.
This function returns an integer for the number of days in the month of February. I used this function in a custom
calendar program I originally written some years for ago for Visual Basic and ported to Gambas to populate the month of
February with the correct number of days.
PUBLIC FUNCTION LeapYear(qYEAR AS Integer) AS Integer
'######################################################################################
'# A function to check the year and determine if it is a leap #
'# year or not and return the number days in the month of February. #
'# qYEAR (queryYEAR) is the four digit year to be analyzed. #
'# Written by Ongytenes 2000 Released under the Creative Commons Liscense. #
'######################################################################################
If qYEAR Mod 4 <> 0 Then
Return 28
Else
If qYEAR Mod 400 = 0 Then
Return 29
Else
If qYEAR Mod 100 = 0 Then
Return 28
Else
Return 29
End If
End If
End If
END
This is a function that returns a Boolean value. True if the year is a leap year; False if the year isn't.
PUBLIC FUNCTION LeapYear(qYEAR AS Integer) AS Boolean
'######################################################################################
'# A function to check the year and determine if it is a leap #
'# year or not and return a Boolean value. True for a Leap Year; False if not. #
'# qYEAR (queryYEAR) is the four digit year to be analyzed. #
'# Written by Ongytenes - 2000 Released under the Creative Commons Liscense. #
'######################################################################################
IF qYEAR MOD 4 <> 0 THEN
RETURN False
ELSE
IF qYEAR MOD 400 = 0 THEN
RETURN True
ELSE
IF qYEAR MOD 100 = 0 THEN
RETURN False
ELSE
RETURN True
END IF
END IF
END IF
END
If you want to try one of the functions just create a button on a form.
For the first function you can enter the following code to call the function LeapYear. The number 28 or 29 will be return.
PUBLIC Sub Button1_Click()
Dim FebruaryDayCount AS Integer
FebruaryDayCount = LeapYear(Year(NOW))
Message.Info("Number of days: " & FebruaryDayCount, "Ok")
END
For the second function you can enter the following code. The Boolean value of True or False will be return.
PUBLIC Sub Button1_Click()
Dim IsLeapYear AS Boolean
Dim qYear as integer
qyear = 2015
IsLeapYear = LeapYear(qyear))
IF IsleapYear = True then
Message.Info("The year " & Str(Year(Now)) & " is a leap year", "Ok")
ELSE
Message.Info("The year " & Str(Year(Now)) & " is not a leap year", "Ok")
ENDIF
END