如何以更易读的格式格式化Rails时间戳?如果我只是在我的视图中打印出created_at
或updated_at
,如下所示:
<% @created = scenario.created_at %>
然后我会得到:
2009-03-27 23:53:38 UTC
发布于 2009-03-29 01:42:06
strftime
(来自Ruby的Time)和to_formatted_s
(来自Rails的ActiveSupport)函数应该能够处理所有的时间格式化需求。
发布于 2011-04-28 15:54:19
看一看I18n的功能。它允许您在视图中执行以下操作:
<%= localize(scenario.created_at, :format => :long) %>
在您的语言环境中定义格式的位置。More info
发布于 2012-05-22 21:35:42
也是
<%= l scenario.created_at, :format => :sample) %>
和locales/en.yml中(取决于语言)
en:
time:
formats:
sample: '%d.%m.%Y'
要了解更多信息,请参阅- http://guides.rubyonrails.org/i18n.html
https://stackoverflow.com/questions/693823
复制相似问题