class Faker::Date

def backward(days: 365)

Returns:
  • (Date) -

Parameters:
  • days (Integer) -- The maximum number of days to go into the past.
def backward(days: 365)
  from = ::Date.today - days
  to   = ::Date.today - 1
  between(from: from, to: to).to_date
end

def between(from:, to:)

Other tags:
    Example: if used with Rails (Active Support) -
    Example: if used with or without Rails (Active Support) -

Returns:
  • (Date) -

Parameters:
  • to (Date, String) -- The end of the usable date range.
  • from (Date, String) -- The start of the usable date range.
def between(from:, to:)
  from = get_date_object(from)
  to   = get_date_object(to)
  Faker::Base.rand_in_range(from, to)
end

def between_except(from:, to:, excepted:)

Other tags:
    Example: if used with Rails (Active Support) -
    Example: if used with or without Rails (Active Support) -

Returns:
  • (Date) -

Parameters:
  • excepted (Date, String) -- A date to exclude.
  • to (Date, String) -- The end of the usable date range.
  • from (Date, String) -- The start of the usable date range.
def between_except(from:, to:, excepted:)
  raise ArgumentError, 'From date, to date and excepted date must not be the same' if from == to && to == excepted
  excepted = get_date_object(excepted)
  loop do
    date = between(from: from, to: to)
    break date.to_date if date != excepted
  end
end

def birthday(min_age: 18, max_age: 65)

Returns:
  • (Date) -

Parameters:
  • max_age (Integer) -- The maximum age that the birthday would imply.
  • min_age (Integer) -- The minimum age that the birthday would imply.
def birthday(min_age: 18, max_age: 65)
  t = ::Date.today
  from = birthday_date(t, max_age)
  to   = birthday_date(t, min_age)
  between(from: from, to: to).to_date
end

def birthday_date(date, age)

def birthday_date(date, age)
  year = date.year - age
  day =
    if date.day == 29 && date.month == 2 && !::Date.leap?(year)
      28
    else
      date.day
    end
  ::Date.new(year, date.month, day)
end

def forward(from: ::Date.today, days: 365)

Other tags:
    Example: if used with or without Rails (Active Support) -
    Example: if used with Rails (Active Support) -
    Example: if used with or without Rails (Active Support) -

Returns:
  • (Date) -

Parameters:
  • days (Integer) -- The maximum number of days to go into the future.
  • from (Integer) -- The start of the usable forward date range.
def forward(from: ::Date.today, days: 365)
  start_date = get_date_object(from)
  since = start_date + 1
  to = start_date + days
  between(from: since, to: to).to_date
end

def get_date_object(date)

def get_date_object(date)
  date = ::Date.parse(date) if date.is_a?(::String)
  date = date.to_date if date.respond_to?(:to_date)
  date
end

def in_date_period(month: nil, year: ::Date.today.year)

Returns:
  • (Date) -

Parameters:
  • year (Integer) -- represents the year of the date
  • month (Integer) -- represents the month of the date
def in_date_period(month: nil, year: ::Date.today.year)
  from = ::Date.new(year, month || 1, 1)
  to = ::Date.new(year, month || 12, ::Date.civil(year, month || 12, -1).day)
  between(from: from, to: to).to_date
end

def on_day_of_week_between(day:, from:, to:)

Other tags:
    Example: if used with Rails (Active Support) -
    Example: if used with or without Rails (Active Support) -

Returns:
  • (Date) -

Parameters:
  • to (Date, String) -- The end of the usable date range.
  • from (Date, String) -- The start of the usable date range.
  • day (Symbol, Array) -- # The day(s) of the week. See {DAYS_OF_WEEK}.
def on_day_of_week_between(day:, from:, to:)
  days = [day].flatten
  raise ArgumentError, 'Day of week cannot be empty' if days.empty?
  # Convert given days of the week to numbers used by `Date#wday` method
  numeric_weekdays = days.map do |d|
    DAYS_OF_WEEK.index(d.to_sym.downcase) || raise(ArgumentError, "#{d} is not a valid day of the week")
  end
  from = get_date_object(from)
  to   = get_date_object(to)
  date = Faker::Base.rand_in_range(from, to)
  # If the initial date is not on one of the wanted days of the week...
  unless numeric_weekdays.include? date.wday
    # ...pick a date nearby that is on one of the wanted days of the week instead
    date += sample(numeric_weekdays) - date.wday
    # Move date 1 week earlier or later if the adjusted date is now outside the date range
    date += 7 if date < from
    date -= 7 if date > to
    if date > to || date < from
      raise ArgumentError,
            "There is no #{DAYS_OF_WEEK[date.wday].capitalize} between #{from} and #{to}. Increase the from/to date range or choose a different day of the week."
    end
  end
  date
end