Rails 3没有UTC的默认datetime格式 [英] Rails 3 default datetime format without UTC

查看:108
本文介绍了Rails 3没有UTC的默认datetime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的Rails 3应用程序,在其中我使用 DateTime 的几个字段,但每个datetime字段标准都有UTC后面查看),如:

  2010-10-10 16:19:00 UTC 

如何摆脱UTC部分?



更新:这是我有到目前为止:

 <%= trip.truckleft.strftime(%Y-%m-%d%H:% M)%> 

所以我现在要做的就是把它放在一个帮手中,但是没有更好的更普遍的方式?



我看了一些其他的帖子,建议在初始化器中创建一个 time_formats.rb 感谢您的帮助,非常感谢!

解决方案

另一个 - 也许现在的首选方式是使用。在该指南中有很多要学习的内容,所以tl; dr版本是这样的:

 <%= l trip.truckleft ,:format => :长%> 

一些预定义的日期和时间格式,如:long 已经是英文,你可以通过在这些例子中遵循YAML结构,在 config / locales / en.yml 中添加你自己的。如果你还没有深入整个i18n / l10n的东西,而且看着 l 方法一直都很混乱,你也可以使用:

 <%= trip.truckleft.to_formatted_s(:long)%> 


I'm creating a new Rails 3 app, and in it I use DateTime for a couple of fields, however every datetime field standard has UTC behind it (in a view), like:

2010-10-10 16:19:00 UTC

How do I get rid of the UTC part?

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.rb in initializers, however I didn't have any success doing that.

Thanks for your help, much appreciated!

解决方案

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:

<%= l trip.truckleft, :format => :long %>

There are a few predefined date and time formats like :long available to you already for English, and you can add your own in config/locales/en.yml by 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 l method all the time is confusing, you can also use:

<%= trip.truckleft.to_formatted_s(:long) %>

这篇关于Rails 3没有UTC的默认datetime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆