Ruby-on-rails 格式化时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/693823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:04:43  来源:igfitidea点击:

Formatting timestamps

ruby-on-railsformattingtimestamp

提问by alamodey

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

您如何以更易于阅读的格式格式化 Rails 时间戳?如果我只是打印出来created_atupdated_at在我看来是这样的:

<% @created = scenario.created_at %>

Then I will get:

然后我会得到:

2009-03-27 23:53:38 UTC

2009-03-27 23:53:38 UTC

回答by Greg Campbell

The strftime(from Ruby's Time) and to_formatted_s(from Rails' ActiveSupport) functions should be able to handle all of your time-formatting needs.

strftime(从Ruby的时间)和to_formatted_s(从Rails的的ActiveSupport)函数应该能够处理所有的时间格式化的需求。

回答by acw

Take a look at the I18n functionality. It allows you to do the following in your views:

看看 I18n 功能。它允许您在视图中执行以下操作:

<%= localize(scenario.created_at, :format => :long) %>

where the formats are defined in your locales. More info

在您的语言环境中定义格式的地方。 更多信息

回答by wildDAlex

Also

<%= l scenario.created_at, :format => :sample) %>

And in locales/en.yml(depending of language)

并在 locales/en.yml(取决于语言)

  en:
    time:
      formats:
        sample: '%d.%m.%Y'

To learn more, see - http://guides.rubyonrails.org/i18n.html

要了解更多信息,请参阅 - http://guides.rubyonrails.org/i18n.html

回答by Devaroop

Time.now().to_i works great. For reverse conversion use Time.at(argument)

Time.now().to_i 效果很好。对于反向转换使用 Time.at(argument)

回答by ekauffmann

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

您可以使用strftime多种方式来格式化时间戳。我更喜欢some_data[:created_at].strftime('%F %T')%F显示“2017-02-08”(日历日期延长),并%T显示“08:37:48”(当地时间延长)。

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

对于时区问题,将此行添加到您的config/application.rb文件中

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

回答by user3803150

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:

您必须修改时间戳文件,在我的情况下,此文件位于/usr/local/rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.2.0/lib/active_record/timestamp.rb. 您必须搜索此行:

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 strftimemethod, you can change the format if you want.

诀窍是使用strftime方法修改格式,您可以根据需要更改格式。

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

现在 rails 将使用您的格式来更新“updated_at”列。