Skip to content

Instantly share code, notes, and snippets.

@lujanfernaud
Last active June 19, 2018 16:55
Show Gist options
  • Save lujanfernaud/3b53a36912b169e1a592934abbe7abdb to your computer and use it in GitHub Desktop.
Save lujanfernaud/3b53a36912b169e1a592934abbe7abdb to your computer and use it in GitHub Desktop.
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