class Date

Public Instance Methods

random(days) click to toggle source

It will generate a random date In case days is a Date it will generate until that date In case days is an Integer it will generate from the self date + the number of days specified examples:

puts Date.today.random(60) # random date from today to 60 days after
puts Date.strptime('01-09-2005', '%d-%m-%Y').random(100)
puts Date.new(2003,10,31).random(Date.today) #Random date from 2003/10/31 to today
# File lib/nice/hash/add_to_ruby.rb, line 126
def random(days)
  if days.is_a?(Date)
    dif_dates = self - (days + 1)
    date_result = self + rand(dif_dates)
    date_result
  elsif days.is_a?(Integer)
    date_result = self + rand(days + 1)
    date_result
  end
end