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

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

问题描述

我正在创建一个新的 Rails 3 应用程序,在其中我将 DateTime 用于几个字段,但是每个日期时间字段标准背后都有 UTC(在视图中),例如:

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

如何去掉UTC部分?

更新:这是我目前所拥有的:

UPDATE: here's what I have so far:

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

所以我现在要做的就是把它放在一个 helper 中,但没有更好更通用的方法吗?

So all I have to do now is put that in a helper, but isn't there a better more universal way?

我查看了其他一些帖子,建议在初始化程序中创建一个 time_formats.rb,但我没有成功.

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!

推荐答案

另一种——也许现在更喜欢——的方法是使用 Rails 的国际化和本地化支持.该指南中有很多东西需要学习,所以 tl;dr 版本是这样的:

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 %>

一些预定义的日期和时间像 :long 这样的格式,你已经可以使用英语了,你可以按照那些中的 YAML 结构在 config/locales/en.yml 中添加你自己的格式例子.如果您还没有深入了解整个 i18n/l10n 的内容并且一直在查看 l 方法令人困惑,您也可以使用:

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) %>

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

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