Skip to content

Instantly share code, notes, and snippets.

@JohnathanWeisner
Created December 18, 2014 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnathanWeisner/212170d46702f7713733 to your computer and use it in GitHub Desktop.
Save JohnathanWeisner/212170d46702f7713733 to your computer and use it in GitHub Desktop.
require 'date'
def random_date_between(args)
start, finish = args[:start], args[:finish]
start, finish = [start, finish].map do |datetime|
case
when datetime.is_a?(DateTime) then datetime.to_time.to_i
when datetime.is_a?(Date) then DateTime.parse(datetime.to_s).to_time.to_i
when datetime.is_a?(String) then DateTime.parse(datetime).to_time.to_i
else
raise "Cant use dates of #{datetime.class} class"
end
end
DateTime.strptime((rand(finish - start) + start).to_s, "%s")
end
p random_date_between(start: "1970/1/1", finish: "2014/12/18")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment