Created
December 18, 2014 22:37
-
-
Save JohnathanWeisner/212170d46702f7713733 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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