Formatting timestamps

后端 未结 6 1324
旧时难觅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条回答
  •  悲哀的现实
    2021-02-12 12:46

    you have to modify the timestamp file, in my case this file is located in /usr/local/rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.2.0/lib/active_record/timestamp.rb. You must search for this line:

    self.class.default_timezone == :utc ? Time.now.utc : Time.now
    

    and change it to this:

    self.class.default_timezone == :utc ? Time.now.utc : Time.now.strftime('%Y-%m-%d %H-%M-%S')
    

    The trick is to modify the format with the strftime method, you can change the format if you want.

    Now rails will use your format to update the "updated_at" column.

提交回复
热议问题