如何更改 created_at 格式
How to change created_at format
这应该是一件简单的事情。好吧,我确定它应该很简单,这是 rails。
问题是:模型中所有数据都有一个字段created_at。在我使用块的视图中检索此信息,其中一行是 t.created_at.
它显示的结果像 2015-04-12 11:04:44 UTC
我应该使用哪种方法将此日期显示为 2015-04-12?
我想应该是这样的:
t.created_at.date_only
你能帮帮我吗?
您可以阅读更多关于 strftime
here
t.created_at.strftime("%Y-%m-%d")
还有更简单的方法:
t.created_at.to_date
或使用区域设置:
// view:
<% @articles.each do |c| %>
<%= l c.created_at.to_date, format: :short %>
<% end %>
有时我们会求助于以下技巧,但我不推荐它
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def created_at
attributes['created_at'].strftime("%Y-%m-%d %H:%M")
end
end
这应该是一件简单的事情。好吧,我确定它应该很简单,这是 rails。
问题是:模型中所有数据都有一个字段created_at。在我使用块的视图中检索此信息,其中一行是 t.created_at.
它显示的结果像 2015-04-12 11:04:44 UTC
我应该使用哪种方法将此日期显示为 2015-04-12? 我想应该是这样的: t.created_at.date_only
你能帮帮我吗?
您可以阅读更多关于 strftime
here
t.created_at.strftime("%Y-%m-%d")
还有更简单的方法:
t.created_at.to_date
或使用区域设置:
// view:
<% @articles.each do |c| %>
<%= l c.created_at.to_date, format: :short %>
<% end %>
有时我们会求助于以下技巧,但我不推荐它
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def created_at
attributes['created_at'].strftime("%Y-%m-%d %H:%M")
end
end