Ruby on Rails - 如何以我需要的格式显示日期?从 YYYY-MM-DD HH:MM:SS UTC 转换为 MM/DD/YYYY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/766674/
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
Ruby on Rails - how to display a date in format i need? Converting from YYYY-MM-DD HH:MM:SS UTC to MM/DD/YYYY
提问by Bob Aman
I am a RoR newbie. I tried a lot of things, finally came to following:
我是一个 RoR 新手。我尝试了很多东西,最后得出以下结论:
<td>
<%= Date.strptime(request.baseline_start_date, "%Y-%M-%D %H:%M:%S %Z").strftime("%M/%D/%Y")%>
</td>
But this is also giving me an error:
但这也给了我一个错误:
$_ value need to be String (nil given)
But I know that request.baseline_start_date gives me value (tried printing it separately). I don't know which one it is saying as nil given.
但我知道 request.baseline_start_date 给了我价值(尝试单独打印)。我不知道它说的是哪一个是 nil 给定的。
Any suggestions on how I can achieve format conversion?
关于如何实现格式转换的任何建议?
回答by Andrew
In Rails you can use the to_timefunction on a string to convert it into a Date object:
在 Rails 中,您可以to_time在字符串上使用该函数将其转换为 Date 对象:
'2012-11-14 14:27:46'.to_time.strftime('%B %e at %l:%M %p')
#=> "November 14 at 2:27 PM"
For a handy, interactive reference guide, refer to http://www.foragoodstrftime.com/
如需方便的交互式参考指南,请参阅http://www.foragoodstrftime.com/
回答by Bob Aman
Date.strptime(
"2009-04-24 18:33:41 UTC",
"%Y-%m-%d %H:%M:%S %Z"
).strftime("%m/%d/%Y")
# => "04/24/2009"
I think maybe you just got the capitalization on your format strings wrong.
我想也许你只是把格式字符串的大小写弄错了。
回答by phil88530
Check the active support documentation and examples at: http://apidock.com/rails/ActiveSupport/CoreExtensions/DateTime/Conversions/to_formatted_s
检查活动支持文档和示例:http: //apidock.com/rails/ActiveSupport/CoreExtensions/DateTime/Conversions/to_formatted_s
Examples
例子
datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
datetime.to_s(:db) # => "2007-12-04 00:00:00"
datetime.to_s(:number) # => "20071204000000"
datetime.to_formatted_s(:short) # => "04 Dec 00:00"
datetime.to_formatted_s(:long) # => "December 04, 2007 00:00"
datetime.to_formatted_s(:long_ordinal) # => "December 4th, 2007 00:00"
datetime.to_formatted_s(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
Or if you really want to customise it, define the helper like:
或者,如果您真的想自定义它,请定义帮助程序,如:
def custom_format(time)
Time::DATE_FORMATS[:w3cdtf] = lambda { |time| time.strftime("%Y-%m-%dT%H:%M:%S# {time.formatted_offset}") }
end
回答by insane.dreamer
You can use the String#to_time (or Date#to_time) function in ActiveSupport to convert the string into a Time (or Date) object. Then use strftime as you have already.
您可以使用 ActiveSupport 中的 String#to_time(或 Date#to_time)函数将字符串转换为时间(或日期)对象。然后像你一样使用 strftime 。
回答by Ryan
Ive written a really nice gem that simplifies the whole process, and makes date formatting DRY.
我写了一个非常好的 gem,它简化了整个过程,并使日期格式变得干燥。
Check it out at: http://github.com/platform45/easy_dates
回答by Chad Ruppert
What I have done is add an initializer named conversions.rb in config/initializer After that Add a line like follows:
我所做的是在 config/initializer 中添加一个名为 conversions.rb 的初始化程序,然后添加如下一行:
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:<name> => '<formatting>')
From there on you can render your datetime using your format with:
从那时起,您可以使用以下格式呈现日期时间:
dateVar.to_s(:<name>)
There is a handy list hereof the formatting tokens
有一个方便的名单这里的格式令牌
回答by Chad Ruppert
Thanks a lot for the reply. My problem is, the output seems to be already string and i have to convert from date in string to another format.
非常感谢您的回复。我的问题是,输出似乎已经是字符串,我必须将字符串中的日期转换为另一种格式。
When I look at the date stored in database (Oracle) it is mm/dd/yy, but when i get it displayed, it adds the timestamp and timezone.
当我查看存储在数据库 (Oracle) 中的日期时,它是 mm/dd/yy,但是当我显示它时,它会添加时间戳和时区。
I tried setting the default in Configuration\environment.rb as ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!( :default => '%d %b %Y' ) But that also doesn't seem to help.
我尝试将 Configuration\environment.rb 中的默认设置为 ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!( :default => '%d %b %Y' ) 但这似乎也没有帮助。
At the end, if I just get the string to convert from Timezone format to mm/dd/yyyy, that is enough.
最后,如果我只是让字符串从 Timezone 格式转换为 mm/dd/yyyy,那就足够了。

