Ruby-on-rails 将日期时间转换为月、日和年?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3042801/
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 22:47:24  来源:igfitidea点击:

Transforming Datetime into month, day and year?

ruby-on-railsruby

提问by bgadoci

I can't seem to find this and I feel like it should be easy. In Ruby on Rails, how do I take:

我似乎找不到这个,我觉得这应该很容易。在 Ruby on Rails 中,我该怎么做:

2010-06-14 19:01:00 UTC

and turn it into

并把它变成

June 14th, 2010

Can I not just use a helper in the view?

我不能只在视图中使用助手吗?

回答by Salil

I don't know for

我不知道

June 14th, 2010

But if you want

但如果你想

June 14, 2010

Ref how do i get name of the month in ruby on Rails?or this

参考 如何在 ruby​​ on Rails 中获取月份名称?或者这个

Just do

做就是了

@date = Time.now
@date.strftime("%B %d, %Y")

And for suffix use following

对于后缀使用以下

@date.strftime("%B #{@date.day.ordinalize}, %Y") # >>> Gives `June 18th, 2010`

回答by Zakaria

Time and date formats in rails:

rails 中的时间和日期格式:

Date

日期

====

====

db:‘%Y-%m-%d'   2008-08-20

long_ordinal:‘&proc'      August 20th, 2008

long:‘%B %e, %Y'  August 20, 2008

rfc822:‘%e %b %Y'   20 Aug 2008

number:‘%Y%m%d'     20080820

short:‘%e %b'      20 Aug

DateTime

约会时间

====

====

db:‘%Y-%m-%d'   2008-08-20 16:56:21

long_ordinal:‘&proc'      August 20th, 2008 16:56

long:‘%B %e, %Y'  August 20, 2008 16:56

rfc822:‘%e %b %Y'   Wed, 20 Aug 2008 16:56:21 -0600

number:‘%Y%m%d'     20080820165621

short:‘%e %b'      20 Aug 16:56

Time

时间

====

====

db:‘%Y-%m-%d %H:%M:%S'         2008-08-20 16:56:21

long_ordinal:‘&proc'           August 20th, 2008 16:56

long:‘%B %d, %Y %H:%M'           August 20, 2008 16:56

rfc822:‘%a, %d %b %Y %H:%M:%S %z'  Wed, 20 Aug 2008 16:56:21 -0600

short:‘%d %b %H:%M'               20 Aug 16:56

number:‘%Y%m%d%H%M%S'              20080820165621

time:‘%H:%M'                     16:56

for example:

例如:

<%= news.created_at.strftime("%B %d, %Y %H:%M") %>

Thanks http://onrails.org/2008/08/20/what-are-all-the-rails-date-formats.html

谢谢http://onrails.org/2008/08/20/what-are-all-the-rails-date-formats.html

回答by Jarrod

For future reference: Rails date time formats

供将来参考:Rails 日期时间格式

回答by Alex V

You don't need to save it in a variable.

您不需要将其保存在变量中。

Time.now.strftime("%Y-%m-%d")  # 2013-01-08

回答by glenn Hymanman

Needs the Time module for Time.parseand ActiveSupport for Integer#ordinalize:

需要 Time 模块Time.parse和 ActiveSupport Integer#ordinalize

require 'time'
require 'active_support'

input = '2010-06-14 19:01:00 UTC'
t = Time.parse(input)
date = "%s %s, %d" % [t.strftime("%B"), t.day.ordinalize, t.year]
# => "June 14th, 2010"

回答by edavey

Just the other day there was a similar question. In my answer how do I get name of the month in ruby on Rails?I showed how you can add a custom to_sdefinition in your config/environment.rbfile.

就在前几天,有一个类似的问题。在我的回答中,如何在 ruby​​ on Rails 中获取月份名称?我展示了如何to_sconfig/environment.rb文件中添加自定义定义。

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
 :my_own_long_date_format => "%B %d, %Y")

Now you can call Time.now.to_s(:my_own_long_date_format)from any view to get:

现在您可以Time.now.to_s(:my_own_long_date_format)从任何视图调用以获取:

June 15, 2010

回答by Maxime Boué

Update that is working in Rails 5 :

在 Rails 5 中工作的更新:

<%= l @user.created_at, format: :short %>

Internationalize :

国际化:

<%= I18n.l( @user.created_at, format: :short) %>

You can use :long instead of :short

您可以使用 :long 而不是 :short