Rails formatting date

前端 未结 4 931
攒了一身酷
攒了一身酷 2020-11-27 11:27

I am posting a date to an API and the required format is as follows:

2014-12-01T01:29:18

I can get the date from the model like so:

4条回答
  •  温柔的废话
    2020-11-27 11:48

    Create an initializer for it:

    # config/initializers/time_formats.rb
    

    Add something like this to it:

    Time::DATE_FORMATS[:custom_datetime] = "%d.%m.%Y"
    

    And then use it the following way:

    post.updated_at.to_s(:custom_datetime)
    

    ⚠️ Your have to restart rails server for this to work.

    Check the documentation for more information: http://api.rubyonrails.org/v5.1/classes/DateTime.html#method-i-to_formatted_s

提交回复
热议问题