将Datetime转换为月、日和年?

时间:2022-02-25 21:22:33

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?

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

7 个解决方案

#1


49  

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

Ref如何在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`

#2


6  

For future reference: Rails date time formats

供以后参考:Rails日期时间格式

#3


5  

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

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

#4


3  

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

不需要将它保存在变量中。

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

#5


2  

Needs the Time module for Time.parse and ActiveSupport for Integer#ordinalize:

需要时间模块。解析和ActiveSupport的整数#序化:

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"

#6


0  

Don't forget, ruby is on rails :D

别忘了,ruby on rails:D。

http://ruby-doc.org/core/classes/Time.html#M000298

http://ruby-doc.org/core/classes/Time.html M000298

#7


0  

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_s definition in your config/environment.rb file.

就在前几天,有一个类似的问题。在我的回答中,我如何在ruby on Rails中获得本月的名称?我展示了如何在配置/环境中添加定制的to_s定义。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

#1


49  

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

Ref如何在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`

#2


6  

For future reference: Rails date time formats

供以后参考:Rails日期时间格式

#3


5  

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

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

#4


3  

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

不需要将它保存在变量中。

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

#5


2  

Needs the Time module for Time.parse and ActiveSupport for Integer#ordinalize:

需要时间模块。解析和ActiveSupport的整数#序化:

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"

#6


0  

Don't forget, ruby is on rails :D

别忘了,ruby on rails:D。

http://ruby-doc.org/core/classes/Time.html#M000298

http://ruby-doc.org/core/classes/Time.html M000298

#7


0  

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_s definition in your config/environment.rb file.

就在前几天,有一个类似的问题。在我的回答中,我如何在ruby on Rails中获得本月的名称?我展示了如何在配置/环境中添加定制的to_s定义。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