advent calendar : Date Calculation « Date « Ruby






advent calendar


require 'date'
def advent_calendar(date=DateTime.now)
  christmas = DateTime.new(date.year, 12, 25)
  christmas = DateTime.new(date.year+1, 12, 25) if date > christmas
  difference = (christmas-date).to_i
  if difference == 0
    puts "Today is Christmas."
  else
    puts "Only #{difference} day#{"s" unless difference==1} until Christmas."
  end
end

advent_calendar(DateTime.new(2006, 12, 24))

 








Related examples in the same category

1.Subtract 61 days from a date:
2.Add and subtract six months using the >> and << methods.
3.Doing Date Arithmetic
4.Doing DateTime Arithmetic
5.Minus one DateTime from another
6.Move DateTime
7.leap year day
8.Counting the Days Since an Arbitrary Date
9.Minus DateTime by day
10.Remaining days