Formatting Dates in Rails 3.0
-
24-10-2019 - |
Question
I'm trying to format a date in Rails 3 using the new syntax as described in the code:
by using the following syntax in an initialiser:
Date::DATE_FORMATS[:my_format] = '%m %d %Y'
I am then referencing dates in my view like so:
comment.created_at.to_formatted_s(:my_format)
However, I'm just getting the default formatting - what could be wrong?
Solution 2
It would appear that the following 'to_date' is required:
comment.created_at.to_date.to_formatted_s(:my_format)
OTHER TIPS
There are two classes, Time and Date.
If you have a time object you'd use:
Time::DATE_FORMATS[:my_format] = '%m %d %Y'
If you have a date object you'd use:
Date::DATE_FORMATS[:my_format] = '%m %d %Y'
So the answer is to put both styles in your initializer file.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow