Ruby on rails 如何在Ruby中生成随机日期?

Ruby on rails 如何在Ruby中生成随机日期?,ruby-on-rails,ruby,ruby-on-rails-3,date,random,Ruby On Rails,Ruby,Ruby On Rails 3,Date,Random,我的Rails 3应用程序中有一个模型,它有一个date字段: class CreateJobs < ActiveRecord::Migration def self.up create_table :jobs do |t| t.date "job_date", :null => false ... t.timestamps end end ... end class CreateJobsfalse ... t、 时间

我的Rails 3应用程序中有一个模型,它有一个
date
字段:

class CreateJobs < ActiveRecord::Migration
  def self.up
    create_table :jobs do |t|
      t.date "job_date", :null => false
      ...
      t.timestamps
    end
  end
  ...
end
class CreateJobsfalse
...
t、 时间戳
结束
结束
...
结束
我想用随机日期值预填充我的数据库

生成随机日期的最简单方法是什么?

试试以下方法:

Time.at(rand * Time.now.to_i)

以下是对Chris答案的略微扩展,其中包含可选的
from
to
参数:

def time_rand from = 0.0, to = Time.now
  Time.at(from + rand * (to.to_f - from.to_f))
end

> time_rand
 => 1977-11-02 04:42:02 0100 
> time_rand Time.local(2010, 1, 1)
 => 2010-07-17 00:22:42 0200 
> time_rand Time.local(2010, 1, 1), Time.local(2010, 7, 1)
 => 2010-06-28 06:44:27 0200 
这里还有一个更完善的Mladen代码片段版本(在我看来)。幸运的是,Ruby的rand()函数也可以处理时间对象。关于包含Rails时定义的日期对象,rand()方法会被覆盖,因此它也可以处理日期对象。e、 g:

# works even with basic ruby
def random_time from = Time.at(0.0), to = Time.now
  rand(from..to)
end

# works only with rails. syntax is quite similar to time method above :)
def random_date from = Date.new(1970), to = Time.now.to_date
  rand(from..to)
end
编辑:这段代码在ruby v1.9.3之前不起作用

Date.today-rand(10000) #for previous dates

Date.today+rand(10000) #for future dates
另外,增加/减少“10000”参数会更改可用日期的范围

rand(Date.civil(1990, 1, 1)..Date.civil(2050, 12, 31))
我最喜欢的方法

def random_date_in_year(year)
  return rand(Date.civil(year.min, 1, 1)..Date.civil(year.max, 12, 31)) if year.kind_of?(Range)
  rand(Date.civil(year, 1, 1)..Date.civil(year, 12, 31))
end
然后使用like

random_date = random_date_in_year(2000..2020)

下面是我的一条在过去30天内生成随机日期的直线(例如):


对我的lorem ipsum很有用。显然,分钟和秒是固定的。

对于Ruby/Rails的最新版本,您可以在
时间范围内使用
rand
❤️ !!

min_date = Time.now - 8.years
max_date = Time.now - 1.year
rand(min_date..max_date)
# => "2009-12-21T15:15:17.162+01:00" (Time)
您可以随意将
添加到_date
添加到_datetime
等,以转换为您喜爱的课程


在Rails 5.0.3和Ruby 2.3.3上进行了测试,但显然可以从Ruby 1.9+和Rails 3+上获得Mladen的答案有点难以理解。这是我的看法

def time_rand from=0, to= Time.now
  Time.at(rand(from.to_i..to.to_i))
end

下面返回Ruby(sans Rails)中过去3周内的随机日期时间


DateTime.now-(rand*21)
由于您使用的是Rails,因此可以安装
faker
gem并使用该模块

e、 g.以下生成2018年的随机日期:


Faker::Date.between(Date.parse('01/01/2018')、Date.parse('31/12/2018'))

对我来说,最漂亮的解决方案是:

rand(1.year.ago..50.weeks.from_now).to_date

你不能现在就开始计时吗?或者你真的需要使用随机日期吗?我真的想要随机值:)这将生成一个从历元到现在的随机日期谢谢!它很短,而且很简单,很有效。只是想知道它是否可以简化为
Time.at(rand*Time.now.to_f)
?我一直想知道这何时会成为ruby核心的一部分!比如
rand(date1..date2)
!顺便说一句,JRuby中现在有一个bug,所以我必须在发送到time之前使用一个额外的.to_time.at--为了让它在JRuby 1.7.4中工作,就像这样:
time.at((from+rand*(to.to_f-from.to_f)).to_time)
这是返回时间,而不是日期。要返回日期,只需在末尾添加“to_date”。(日期和时间之间的比较会给你带来rails中的一个错误)@VarunVohra Shit这实际上已经是我在rails 5/Ruby 2.3上的核心(不确定它来自何方)它可以简单地写成
Date.today rand(10000)
Date.today+rand(10000)
rand(1.year.ago..50.weeks.from_now).to_date