How do get a random DateTime rounded to beginning of hour in Rails?

Viewed 15096

Basically I'd like to get a random datetime within the last year:

rand(1.year).ago  #=> Sun, 22 Sep 2013 18:37:44 UTC +00:00 (example)

But how do I go about specifying or limiting this to times on the hour? For example:

Sun, 22 Sep 2013 18:00:00 UTC +00:00
Sat, 02 Nov 2013 10:00:00 UTC +00:00
Fri, 12 Apr 2013 21:00:00 UTC +00:00
3 Answers
Related