Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails: Random Date in Period

Rails: Random Date in Period

def random_date_in_period(period)
  rand(period).seconds.ago
end

random_date_in_period 1.year #=> Tue, 22 May 2018 04:55:54 UTC +00:00
random_date_in_period 1.year #=> Mon, 06 Nov 2017 19:50:07 UTC +00:00

random_date_in_period 1.week #=> Sat, 16 Jun 2018 18:07:31 UTC +00:00
random_date_in_period 1.week #=> Wed, 13 Jun 2018 19:02:37 UTC +00:00

Kudos to Marc O'Morain.
Source: https://stackoverflow.com/a/4886512/6212572

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.