0

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?

kid_drew
  • 3,857
  • 6
  • 28
  • 38
  • 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 Answers2

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"
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"