Reputation: 386
I'm using factory girl and faker to generate data for a rails app. The trouble is, faker's docs don't say anything about generating random dates.
Here is my existing code. As you can see, I'm unsure of how to proceed after start_date. How can I generate a date?
Thanks in advance :)
FactoryGirl.define do
factory :registration_form do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
email Faker::Internet.safe_email
phone_number Faker::PhoneNumber.phone_number
twitter Faker::Internet.user_name
skype Faker::Internet.user_name
start_date Faker:: ???????????
end
end
Upvotes: 2
Views: 11557
Reputation: 47601
Not sure when they put this in but the latest version supports Times, which you can convert to a Date quite easily, if that's required.
Here are some key methods:
# Random date between dates
Faker::Time.between(from: 2.days.ago, to: Time.now) #=> "2014-09-18 12:30:59 -0700"
# Random date between dates (within specified part of the day)
Faker::Time.between(from: 2.days.ago, to: Time.now, period: :all) #=> "2014-09-19 07:03:30 -0700"
Faker::Time.between(from: 2.days.ago, to: Time.now, period: :day) #=> "2014-09-18 16:28:13 -0700"
Faker::Time.between(from: 2.days.ago, to: Time.now, period: :night) #=> "2014-09-20 19:39:38 -0700"
Faker::Time.between(from: 2.days.ago, to: Time.now, period: :morning) #=> "2014-09-19 08:07:52 -0700"
Faker::Time.between(from: 2.days.ago, to: Time.now, period: :afternoon) #=> "2014-09-18 12:10:34 -0700"
Faker::Time.between(from: 2.days.ago, to: Time.now, period: :evening) #=> "2014-09-19 20:21:03 -0700"
Faker::Time.between(from: 2.days.ago, to: Time.now, period: :midnight) #=> "2014-09-20 00:40:14 -0700"
# Random time in the future (up to maximum of N days)
Faker::Time.forward(days: 23, period: :morning) #=> "2014-09-26 06:54:47 -0700"
# Random time in the past (up to maximum of N days)
Faker::Time.backward(days: 14, period: :evening) #=> "2014-09-17 19:56:33 -0700"
Upvotes: 8
Reputation: 9317
You can generate random dates like below
Faker::Date.between(2.days.ago, Date.today) #=> "Wed, 24 Sep 2014"
You can find more options here https://github.com/stympy/faker/blob/master/doc/default/date.md
Upvotes: 0
Reputation: 1525
Use the credit card expiration date:
Faker::Business.credit_card_expiry_date
Upvotes: 1
Reputation: 3500
Instead of usin Faker, you could render a random Date withou faker, by usin
Time.at(0.0 + rand * (Time.now.to_f - 0.0.to_f)).to_date
AFAIK it is faster and you can use it in many more ways. If you need another Range than 1970-01-01 to today, you can also use
Time.at(Time.new(1990).to_f + rand * (Time.now.to_f - Time.new(1990).to_f)).to_date
Upvotes: 0
Reputation:
I would use Faker::Number.number
to vary the date relative to today.
e.g.
start_date { Date.today - Faker::Number.number(3).to_i.days }
Alternatively, if you don't want to use Faker, you could generate random future or past dates like this:
rand(1.year).from_now
rand(1.year).ago
Upvotes: 3
Reputation: 1021
The faker gem does have as far as I know one date use and that is for credit card expiration date.
Depending on the usage, you can just set dates in your factory. This could be like:
start_date Date.today + 7.days (1.week, 1.month etc)
Upvotes: 1