rubydatetimeleap-year

Easy way to determine leap year in ruby?


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


Solution

  • Use Date#leap?.

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

    e.g.

    Date.leap?( 2018 ) # => false
    
    Date.leap?( 2016 ) # => true