Rails formatting date

前端 未结 4 933
攒了一身酷
攒了一身酷 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:50

    Since I18n is the Rails core feature starting from version 2.2 you can use its localize-method. By applying the forementioned strftime %-variables you can specify the desired format under config/locales/en.yml (or whatever language), in your case like this:

    time:
      formats:
        default: '%FT%T'
    

    Or if you want to use this kind of format in a few specific places you can refer it as a variable like this

    time:
      formats:
        specific_format: '%FT%T'
    

    After that you can use it in your views like this:

    l(Mode.last.created_at, format: :specific_format)  
    

提交回复
热议问题