Countdown dates in Ruby

Hi all.

I’m making a program in Ruby that gets 2 dates, calculates which is the
oldest and the newest, and have to do a countdown of days, months and
years to the oldest date.

The countdown should be done using a date format. for example. mm / dd /
yyyy, not showing the years, months and days.

I tried it but I can not think how to do this program?

I accept any suggestions. thanks

Hi,

You can use the Date class from the standard library. It has all the
necessary methods:

  • Date.parse to create a Date object from a string
  • Date#<=>, so that Dates can be compared
  • Date#downto to iterate over the timespan (in days) between two dates
  • Date#strftime to get a string representation according to a format

#------------------------------
require ‘date’

raw_dates = [‘2012-06-03’, ‘2011-12-22’]

date_old, date_new =
raw_dates.map{|str| Date.parse str}.sort

date_new.downto date_old do |date|
puts date.strftime ‘%m / %d / %Y’
end
#------------------------------

See

for the documentation.