开发者

How to return my current time zone in RoR?

开发者 https://www.devze.com 2022-12-17 23:09 出处:网络
When I use return the time that the record created, it show this : 2010-01-20 15:04:40 UTC but I want the time in my specify time zone, for example, China. Is there any convenient method in 开发者

When I use return the time that the record created, it show this :

2010-01-20 15:04:40 UTC

but I want the time in my specify time zone, for example, China. Is there any convenient method in 开发者_开发技巧RoR?


Configure your time zone in config/environment.rb to have Rails cast all timestamps to this time zone.

config.time_zone = 'Berlin'

As an alternative you can always use something like

Time.utc(2000).in_time_zone('Alaska') 

See the documentation here.


Take a look at the TimeZone and TimeWithZone classes. They add time zone support. There's also been some additions to the Time and DateTime classes that also help deal with time zones. The documentation is given here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html.

There's also an excellent post here giving some extra details: http://ryandaigle.com/articles/2008/1/25/what-s-new-in-edge-rails-easier-timezones

0

精彩评论

暂无评论...
验证码 换一张
取 消