Ruby-on-rails 在 Rails 中创建随机日期时间的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2410639/
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
Best way to create random DateTime in Rails
提问by HymanCA
What is the best way to generate a random DateTime in Ruby/Rails? Trying to create a nice seeds.rb file. Going to use it like so:
在 Ruby/Rails 中生成随机 DateTime 的最佳方法是什么?试图创建一个不错的seeds.rb 文件。打算像这样使用它:
Foo.create(name: Faker::Lorem.words, description: Faker::Lorem.sentence, start_date: Random.date)
采纳答案by Harish Shetty
Here are set of methods for generating a random integer, amount, time/datetime within a range.
以下是一组用于在范围内生成随机整数、数量、时间/日期时间的方法。
def rand_int(from, to)
rand_in_range(from, to).to_i
end
def rand_price(from, to)
rand_in_range(from, to).round(2)
end
def rand_time(from, to=Time.now)
Time.at(rand_in_range(from.to_f, to.to_f))
end
def rand_in_range(from, to)
rand * (to - from) + from
end
Now you can make the following calls.
现在您可以进行以下调用。
rand_int(60, 75)
# => 61
rand_price(10, 100)
# => 43.84
rand_time(2.days.ago)
# => Mon Mar 08 21:11:56 -0800 2010
回答by Marc O'Morain
Here is how to create a date in the last 10 years:
以下是如何创建过去 10 年的日期:
rand(10.years).ago
You can also get a date in the future:
您还可以获得未来的日期:
rand(10.years).from_now
Update – Rails 4.1+
更新 – Rails 4.1+
Rails 4.1 has deprecated the implicit conversion from Numeric => seconds when you call .ago, which the above code depends on. See Rails PR #12389for more information about this. To avoid a deprecation warning in Rails 4.1 you need to do an explicit conversion to seconds, like so:
Rails 4.1 已经弃用了 Numeric => seconds 当你调用时的隐式转换.ago,上面的代码依赖于它。有关这方面的更多信息,请参阅Rails PR #12389。为了避免 Rails 4.1 中的弃用警告,您需要显式转换为秒,如下所示:
rand(10.years).seconds.ago
回答by shingara
I prefer use (1..500).to_a.rand.days.ago
我更喜欢使用 (1..500).to_a.rand.days.ago
回答by lucas
You are using Faker; why not use one of the methods provided by Faker::Date?
您正在使用 Faker;为什么不使用提供的方法之一Faker::Date?
# Random date between dates
# Keyword arguments: from, to
Faker::Date.between(from: 2.days.ago, to: Date.today) #=> "Wed, 24 Sep 2014"
# Random date between dates except for certain date
# Keyword arguments: from, to, excepted
Faker::Date.between_except(from: 1.year.ago, to: 1.year.from_now, excepted: Date.today) #=> "Wed, 24 Sep 2014"
# Random date in the future (up to maximum of N days)
# Keyword arguments: days
Faker::Date.forward(days: 23) # => "Fri, 03 Oct 2014"
# Random date in the past (up to maximum of N days)
# Keyword arguments: days
Faker::Date.backward(days: 14) #=> "Fri, 19 Sep 2014"
# Random birthday date (maximum age between 18 and 65)
# Keyword arguments: min_age, max_age
Faker::Date.birthday(min_age: 18, max_age: 65) #=> "Mar, 28 Mar 1986"
# Random date in current year
Faker::Date.in_date_period #=> #<Date: 2019-09-01>
# Random date for range of year 2018 and month 2
# Keyword arguments: year, month
Faker::Date.in_date_period(year: 2018, month: 2) #=> #<Date: 2018-02-26>
# Random date for range of current year and month 2
# Keyword arguments: month
Faker::Date.in_date_period(month: 2) #=> #<Date: 2019-02-26>
current Faker version: 2.11.0
当前 Faker 版本:2.11.0
回答by Ryan Rebo
This is what I use:
这是我使用的:
# get random DateTime in last 3 weeks
DateTime.now - (rand * 21)
回答by Sergey Chechaev
Here is how to create a date in this month:
以下是在本月创建日期的方法:
day = 1.times.map{ 0+Random.rand(30) }.join.to_i
rand(day.days).ago
回答by masterweily
other way:
另一种方式:
(10..20).to_a.sample.years.ago
回答by Paul Yeoh
Another approach using DateTime's advance
使用DateTime 的另一种方法advance
def rand_date
# return a random date within 100 days of today in both past and future directions.
n = rand(-100..100)
Date.today.advance(days: n)
end
回答by Yana Agun Siswanto
You can pass Time Range to rand
您可以将时间范围传递给 rand
rand(10.weeks.ago..1.day.ago)
Output Example:
输出示例:
=> Fri, 10 Jan 2020 10:28:52 WIB +07:00
回答by loybert
As I already mentioned in another question I think the following code-snippet is more consisent regarding the data-types of the parameters and of the value to be returned. Stackoverflow: How to generate a random date in Ruby?
正如我在另一个问题中已经提到的,我认为以下代码片段在参数的数据类型和要返回的值方面更加一致。Stackoverflow:如何在 Ruby 中生成随机日期?
Anyway this uses the rand() method's internal logic what is the random Date or random Time within a span. Maybe someone has a more efficient way to set the default-parameter to(Time.now.to_date) of the method random_date, so it doesn't need this typecasting.
无论如何,这使用 rand() 方法的内部逻辑,什么是跨度内的随机日期或随机时间。也许有人有一个更有效的方式来设置默认参数,以方法(Time.now.to_date)random_date,所以它并不需要此类型转换。
def random_time from = Time.at(0.0), to = Time.now
rand(from..to)
end
# works quite similar to date :)
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 之前不起作用

