I'm building an API, and I'd like to format every datetime in RFC3339 automatically rather than having to specify each manually. Is there an elegant way to do that, or do I just have to override the DateTime#to_s method?
Asked
Active
Viewed 537 times
0
-
Possible duplicate of [Changing the default date and time format in Rails 4](https://stackoverflow.com/questions/17870762/changing-the-default-date-and-time-format-in-rails-4) – Sree Sep 07 '18 at 03:38
2 Answers
2
You could create a file under initializers/time_formats.rb
# config/initializers/time_formats.rb
Date::DATE_FORMATS[:default]="%Y-%m-%d"
Time::DATE_FORMATS[:default]="%Y-%m-%d %H:%M:%S"

Kedarnag Mukanahallipatna
- 2,249
- 1
- 11
- 16
0
Datetime formats can be 'formated' by adding the format to the i18n configuration.
config/locales/en.yml
en:
date:
formats:
default: "%D/%MM/%YYY"
time:
formats:
default: "%d/%m/%Y %H:%M"

Cryptex Technologies
- 1,153
- 9
- 16