Reputation: 1525
In rails I want to check the following condition:
student.bday >= @from_date and student.bday <= @to_date
where @from_date
and @to_date
are determined as:
t = Date.today
@from_date = t.at_beginning_of_week.strftime("%d %b")
@to_date = t.at_end_of_week.strftime("%d %b")
But in the database student.bday
is saved as a string (eg: 17 Aug
). Its not a date field. How can I convert this string (17 Aug
) into ("%d %b")
format or convert to 17 08
or something like that so that I could check the above condition? Any suggestions?
Upvotes: 0
Views: 705
Reputation: 5740
Date.parse("17 Aug")
# Sun, 17 Aug 2014
You may want to bring everything to the same year for the comparison to be effective, just to make sure.
bday = Date.parse("#{student.bday} #{Date.today.strftime('%Y')}")
But default behaviour is to add current year, so this is just redundant...
Better still, provide a parse model, for parsing to be most accurate
bday = Date.strptime("#{student.bday} #{Date.today.strftime('%Y')}",'%d %b %Y')
# %d: day of month, %b: Short month, %Y: Year
Upvotes: 1
Reputation: 503
You can convert your student.bday into the required format as:
Date.parse(student.bday).strftime("%d %m")
This will give the output as "17 08"
Upvotes: 0