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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4894198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:18:59  来源:igfitidea点击:

How to generate a random date in Ruby?

ruby-on-railsrubyruby-on-rails-3daterandom

提问by Misha Moroshko

I have a model in my Rails 3 application which has a datefield:

我的 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

I would like to prepopulate my database with random date values.

我想用随机日期值预填充我的数据库。

What is the easiest way to generate a random date ?

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

回答by Mladen Jablanovi?

Here's a slight expansion on Chris' answer, with optional fromand toparameters:

这是对 Chris 的回答的轻微扩展,带有可选fromto参数:

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 

回答by Chris Heald

Try this:

尝试这个:

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

回答by iGallina

Keeping simple..

保持简单..

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

Date.today+rand(10000) #for future dates

PS. Increasing/Decreasing the '10000' parameter, changes the range of dates available.

附注。增加/减少“10000”参数会改变可用日期的范围。

回答by idrinkpabst

rand(Date.civil(1990, 1, 1)..Date.civil(2050, 12, 31))

my favorite method

我最喜欢的方法

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

then use like

然后使用像

random_date = random_date_in_year(2000..2020)

回答by Cyril Duchon-Doris

For recent versions of Ruby/Rails, You can use randon a Timerange ?? !!

对于最新版本的 Ruby/Rails,您可以randTime范围内使用?? !!

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)

Feel free to add to_date, to_datetime, etc. to convert to your favourite class

随意添加to_date,to_datetime等以转换为您喜欢的类

Tested on Rails 5.0.3 and Ruby 2.3.3, but apparently available from Ruby 1.9+ and Rails 3+

在 Rails 5.0.3 和 Ruby 2.3.3 上测试,但显然可从 Ruby 1.9+ 和 Rails 3+ 获得

回答by Merovex

The following returns a random date-time in the past 3 weeks in Ruby (sans Rails).

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

DateTime.now - (rand * 21)

DateTime.now - (rand * 21)

回答by Markus Andreas

The prettiest solution to me is:

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

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

回答by loybert

Here's also a more (in my oppinion) improved version of Mladen's code-snippet. Luckily Ruby's rand()function can also handle Time-Objects. Regarding the Date-Object is defined when including Rails, the rand() method gets overwritten so it can handle also Date-Objects. e.g.:

这也是 Mladen 代码片段的更多(在我看来)改进版本。幸运的是 Ruby 的rand()函数也可以处理时间对象。关于在包含 Rails 时定义的日期对象,rand() 方法被覆盖,因此它也可以处理日期对象。例如:

# 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

Edit: this code won't work before ruby v1.9.3

编辑:此代码在 ruby​​ v1.9.3 之前不起作用

回答by Matt

Here's my one liner to generate a random date in the last 30 days (for example):

这是我在过去 30 天内生成随机日期的一个班轮(例如):

Time.now - (0..30).to_a.sample.days - (0..24).to_a.sample.hours

Works great for my lorem ipsum. Obviously minutes and seconds would be fixed.

非常适合我的 lorem ipsum。显然分钟和秒是固定的。

回答by dhaliman

Mladen's answer is a little difficult to understand from just a single look. Here's my take on this.

单看一眼,姆拉登的回答有点难以理解。这是我对此的看法。

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