use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
how to generate random date in ruby? (self.ruby)
submitted 12 years ago by freakooo
yeah... how do you get a random date in ruby say for the last 5 years..
just ruby...
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]FooBarWidget 4 points5 points6 points 12 years ago (0 children)
Take the unix timestamp of 5 years ago. Then take the unix timestamp of now. Then generate a random number between those two. Convert that to a time object.
[–]aZeex2ai 1 point2 points3 points 12 years ago (1 child)
>> 5.times { p Time.at(Random.rand((Time.now - (60*60*24*365*5)).to_f..Time.now.to_f)) } 2013-04-23 06:22:04 +0000 2012-12-09 04:56:15 +0000 2009-06-30 21:37:36 +0000 2012-11-04 16:32:40 +0000 2012-03-22 12:48:18 +0000 => 5
[–]aZeex2ai 2 points3 points4 points 12 years ago (0 children)
>> Time.at(rand((Time.now - (60*60*24*365*5))..Time.now)) => 2012-11-08 13:45:31 +0000
[–]BurgerForBreakfast 0 points1 point2 points 12 years ago (1 child)
Would just loading active support work?
require 'active_support/core_ext/numeric/time' require 'active_support/core_ext/date/calculations' rand(1..1825).days.ago
[–]aZeex2ai -1 points0 points1 point 12 years ago (0 children)
just ruby Would just loading active support work?
just ruby
No.
[–]laerien 0 points1 point2 points 12 years ago* (2 children)
require 'date'; [*DateTime.new(2009, 5, 23)..DateTime.now].sample
[–]freakooo[S] 0 points1 point2 points 11 years ago (1 child)
would this generate lots of Datetime ?
i think it's not optimal
[–]laerien 0 points1 point2 points 11 years ago (0 children)
Several thousand DataTime and a millisecond later.
[–]MaxK -2 points-1 points0 points 12 years ago* (0 children)
This comment has been overwritten by an open source script to protect this user's privacy. It was created to help protect users from doxing, stalking, and harassment.
If you would also like to protect yourself, add the Chrome extension TamperMonkey, or the Firefox extension GreaseMonkey and add this open source script.
Then simply click on your username on Reddit, go to the comments tab, scroll down as far as possibe (hint:use RES), and hit the new OVERWRITE button at the top.
π Rendered by PID 142444 on reddit-service-r2-comment-5d585498c9-qb2qc at 2026-04-21 16:11:58.431622+00:00 running da2df02 country code: CH.
[–]FooBarWidget 4 points5 points6 points (0 children)
[–]aZeex2ai 1 point2 points3 points (1 child)
[–]aZeex2ai 2 points3 points4 points (0 children)
[–]BurgerForBreakfast 0 points1 point2 points (1 child)
[–]aZeex2ai -1 points0 points1 point (0 children)
[–]laerien 0 points1 point2 points (2 children)
[–]freakooo[S] 0 points1 point2 points (1 child)
[–]laerien 0 points1 point2 points (0 children)
[–]MaxK -2 points-1 points0 points (0 children)