简体   繁体   中英

How to return my current time zone in RoR?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM