Formatting timestamps

后端 未结 6 1417
旧时难觅i
旧时难觅i 2021-02-12 12:30

How do you format Rails timestamps in a more human-readable format? If I simply print out created_at or updated_at in my view like this:



        
6条回答
  •  萌比男神i
    2021-02-12 12:41

    You can use strftime to format the timestamp in many ways. I prefer some_data[:created_at].strftime('%F %T'). %F shows "2017-02-08" (Calendar date extended), and %T shows "08:37:48" (Local time extended).

    For timezone issues, add this lines to your config/application.rb file

    config.time_zone = 'your_timezone_string'
    config.active_record.default_timezone = :local
    

提交回复
热议问题