Is there an easy way to determine if a year is a leap year?
Any year that is evenly divisible by 4 is a leap year: for example, 1988, 1992, and 1996 are leap years.
If a year is a century year, meaning divisible by 100, then it needs to be divisible by 400 to be called as a leap year. If a year is not a century year, then it needs to be divisible by 4 to be called as a leap year.
The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years, if they are exactly divisible by 400.
To determine when, find the number of new moons between the 11th month in one year and the 11th month in the following year. A leap month is inserted if there are 13 New Moons from the start of the 11th month in the first year to the start of the 11th month in the next year.
Date#leap?
.now = DateTime.now flag = Date.leap?( now.year )
e.g.
Date.leap?( 2018 ) # => false Date.leap?( 2016 ) # => true
For your understanding:
def leap_year?(year) if year % 4 == 0 if year % 100 == 0 if yearVar % 400 == 0 return true end return false end return true end false end
This could be written as:
def leap_year?(year) (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0) end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With