Ruby-on-rails 没有UTC的Rails 3默认日期时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3694352/
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
Rails 3 default datetime format without UTC
提问by Paintrick
I'm creating a new Rails 3 app, and in it I use DateTimefor a couple of fields, however every datetime field standard has UTC behind it (in a view), like:
我正在创建一个新的 Rails 3 应用程序,在其中我DateTime用于几个字段,但是每个日期时间字段标准都在其后面(在视图中),例如:
2010-10-10 16:19:00 UTC
How do I get rid of the UTC part?
如何摆脱 UTC 部分?
UPDATE: here's what I have so far:
更新:这是我到目前为止所拥有的:
<%= trip.truckleft.strftime("%Y-%m-%d %H:%M") %>
So all I have to do now is put that in a helper, but isn't there a better more universal way?
所以我现在要做的就是把它放在一个助手中,但没有更好的更通用的方法吗?
I looked at some other posts, that suggested creating a time_formats.rbin initializers, however I didn't have any success doing that.
我查看了其他一些帖子,建议time_formats.rb在初始化程序中创建一个,但是我没有成功。
Thanks for your help, much appreciated!
感谢您的帮助,非常感谢!
回答by ches
Another -- perhaps now preferred -- way is to use Rails' internationalization and localization support. There's a lot to learn in that guide, so the tl;dr version is this:
另一种(现在可能更喜欢)的方法是使用Rails 的国际化和本地化支持。该指南中有很多东西要学,所以 tl;dr 版本是这样的:
<%= l trip.truckleft, :format => :long %>
There are a few predefined date and time formatslike :longavailable to you already for English, and you can add your own in config/locales/en.ymlby following the YAML structure in those examples. If you're not getting heavily into the whole i18n/l10n thing just yet and looking at the lmethod all the time is confusing, you can also use:
有一些预定义的日期和时间格式,例如:long您已经可以使用英语,您可以config/locales/en.yml按照这些示例中的 YAML 结构添加自己的格式。如果您还没有深入了解整个 i18n/l10n 事情并且一直在查看该l方法令人困惑,您还可以使用:
<%= trip.truckleft.to_formatted_s(:long) %>
回答by Paintrick
Here is what finally worked for me:
这是最终对我有用的方法:
I created a new file in:
我在以下位置创建了一个新文件:
config/initializers/
config/initializers/
named: time_formats.rb
命名:time_formats.rb
and added this to that file:
并将其添加到该文件中:
Time::DATE_FORMATS[:default] = "%Y-%m-%d %H:%M"
Then I saved, restarted the server and it started to work.
然后我保存,重新启动服务器,它开始工作。
回答by jankubr
I'm using i18n to format my dates and have this in en.yml:
我正在使用 i18n 来格式化我的日期并将其保存在 en.yml 中:
date:
formats:
default: "%m/%d/%Y"
I wanted to reuse that format for how the models show their dates, so my config/initializers/time_formats.rb contains this:
我想重复使用该格式来显示模型如何显示其日期,因此我的 config/initializers/time_formats.rb 包含以下内容:
Date::DATE_FORMATS[:default] = lambda { |date| I18n.l(date) }
回答by ?ukasz Anwajler
To be exact, you should put these in your initializers:
确切地说,您应该将这些放在初始化程序中:
Date::DATE_FORMATS[:default] = "%m-%d-%Y"
Time::DATE_FORMATS[:default] = "%m-%d-%Y %H:%M"
When having datetime, the second one will work (for example: created_at for in models).
当有日期时间时,第二个将起作用(例如:created_at for in models)。
回答by okliv
for rails 3
用于导轨 3
add to config/environment.rb
添加 config/environment.rb
my_datetime_formats = { :default => '%F %T' } #or any other you like
my_date_formats = { :default => '%F' } #or any other you like
Time::DATE_FORMATS.merge!(my_datetime_formats)
Date::DATE_FORMATS.merge!(my_date_formats)
(the difference from other answers - is merge!method)
(与其他答案的区别 - 是merge!方法)
回答by Daniel O'Hara
You can put the following line at the end of your config/environment.rbfile:
您可以在config/environment.rb文件末尾添加以下行:
Date::DATE_FORMATS[:default] = "%Y-%m-%d %H:%M"

