30

Is there an easy way to determine if a year is a leap year?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
MikeJ
  • 14,430
  • 21
  • 71
  • 87

11 Answers11

67

Use Date#leap?.

now = DateTime.now 
flag = Date.leap?( now.year ) 

e.g.

Date.leap?( 2018 ) # => false

Date.leap?( 2016 ) # => true
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
10

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
lucasarruda
  • 1,462
  • 1
  • 25
  • 45
9

Try this:

is_leap_year = year % 4 == 0 && year % 100 != 0 || year % 400 == 0
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Pesto
  • 23,810
  • 2
  • 71
  • 76
4

Here is my answer for the exercism.io problem which asks the same question. You are explicitly told to ignore any standard library functions that may implement it as part of the exercise.

class Year
  attr_reader :year

  def initialize(year)
    @year = year
  end

  def leap?
    if @year.modulo(4).zero?
      return true unless @year.modulo(100).zero? and not @year.modulo(400).zero?
    end

    false
  end
end
MattC
  • 12,285
  • 10
  • 54
  • 78
  • Note I said it's for http://exercism.io, which asks you to implement the logic yourself as a coding exercise. – MattC Nov 28 '13 at 01:38
2
def leap_year?(num)
 if num%4 == 0 && num%100 != 0  
    true
 elsif num%400 == 0 
    true
 elsif num%4 == 0 && num%100 == 0 && num%400 != 0 
    false
  elsif num%4 != 0 
    false
  end
end 
puts leap_year?(2000)
sashkello
  • 17,306
  • 24
  • 81
  • 109
CarlosGui
  • 33
  • 1
  • 5
1

Any year that is evenly divisible by 4 is a leap year. However, there is still a small error that must be accounted for. To eliminate this error, the Gregorian calendar stipulates that a year that is evenly divisible by 100 (for example, 1900) is a leap year only if it is also evenly divisible by 400.

class Date

  def self.leap?(year)
    year % 4 == 0 && year % 100 != 0 || year % 400 == 0
  end

end
Artem Sychov
  • 142
  • 1
  • 3
  • 15
  • 1
    Thanks, @DebanjanB I add some explanation to the top - how the formula is built to count the leap year. How do you think should I also add a link that explains the formula in more details? Something like that: https://support.microsoft.com/en-us/help/214019/method-to-determine-whether-a-year-is-a-leap-year – Artem Sychov Feb 23 '19 at 09:32
0

This one takes a range:

(starting..ending).each do |year|
  next if year % 4   != 0
  next if year % 100 == 0 && year % 400 != 0
  puts year
end

Source: Learn to Program by Chris Pine

Matias Fernandez
  • 118
  • 2
  • 11
0

Using the least amount possible of comparisons, you could do this:

  • First/Longer version
def leap_year?(year)
  # check first if year is divisible by 400
  return true if year % 400 == 0
  year % 4 == 0 && year % 100 != 0
end
  • Shorter version

We can do the same check using short circuit OR (||):

def leap_year?(year)
  year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
end
lucasarruda
  • 1,462
  • 1
  • 25
  • 45
0

The variable n takes year to test and prints true if it's a leap year and false if it's not.

n=gets.to_i 
n%4==0 ? n%100==0 ? n%400 ?(puts true):(puts false) :(puts true) :(puts false)
sImAnTiCs
  • 13
  • 2
0

Already a lot of answers, though here is a shorter version:

y%100 > 0 ? y%4 < 1 : y%400 < 1

If the year is not a century, then checks if it is a multiple of 4, otherwise checks if it is multiple of 400.

circular
  • 171
  • 1
  • 5
0

Here's my method:

def leap_year?(year)
  Time.new(year, 12, 31).yday == 366
end

leap_year?(1999)
#=>  false

leap_year?(2000)
#=>  true

leap_year?(2100)
#=>  false

leap_year?(2104)
#=>  true