Rails 3:如何在特定的时区获取今天的日期? [英] Rails 3: How to get today's date in specific timezone?
本文介绍了Rails 3:如何在特定的时区获取今天的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要获取今天的日期,请执行以下操作:
Date.today#=> 2011年5月20日,星期五
我想在特定时区获取今天的日期,说'墨尔本'
。
我的 application.rb中有以下设置
:
config.time_zone ='墨尔本'
,我设置:
Time.zone ='墨尔本'
但是,这并没有什么帮助(我猜这是因为这些设置仅影响数据库中存储的日期)。
$ b在每个动作之前,我的应用程序控制器中都有$ b
我如何在
'墨尔本'
?解决方案你应该可以这样做:
Time.current
。这将显示墨尔本的当前时间,如果这是Time.zone
设置为。To get today's date I do:
Date.today # => Fri, 20 May 2011
I would like to get today's date in a specific timezone, say
'Melbourne'
.I have the following setting in my
application.rb
:config.time_zone = 'Melbourne'
and I set:
Time.zone = 'Melbourne'
in my application controller before each action.
However, it doesn't help (I guess because these settings affects only dates that are stored in the database).
How could I get today's date in
'Melbourne'
?解决方案You should be able to do this:
Time.current
. That would display the current time in Melbourne if that's whatTime.zone
is set to.这篇关于Rails 3:如何在特定的时区获取今天的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文