Rixcy Rixcy - 1 year ago 62
Ruby Question

How to find out if the current date in rails is between a predefined date range (without the year)

I currently have the following code in a helper within rails, which allows me to set a start date and end date and return true based on whether the current date is between the predefined range. My problem is with this code it'll only work for the current year, however I want to change it so it'll return true if the date is within the range, ignoring the year.

def is_christmas?
start_date = Date.parse('01-12-2016')
end_date = Date.parse('25-12-2016')
today = Date.today
today.between?(start_date, end_date)

Any help with this would be greatly appreciated :)

Answer Source

unsure of how to eliminate the year and only check for the day/month

To check for the day:

date.day.between?(1, 25)

To check for the month:

date.month == 12

And to check for both:

date.day.between?(1, 25) && date.month == 12

Wrapped in a method:

def is_christmas?(date = Date.today)
  date.day.between?(1, 25) && date.month == 12
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download