Ruby-on-rails Rails 3:如何在特定时区获取今天的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6060436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:10:51  来源:igfitidea点击:

Rails 3: How to get today's date in specific timezone?

ruby-on-railsruby-on-rails-3datetimezone

提问by Misha Moroshko

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'.

我想在特定时区获取今天的日期,例如'Melbourne'.

I have the following setting in my application.rb:

我有以下设置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'?

我怎么能得到今天的日期'Melbourne'

采纳答案by Ryan Bigg

You should be able to do this: Time.current. That would display the current time in Melbourne if that's what Time.zoneis set to.

你应该能够做到这一点:Time.current。如果这Time.zone是设置的,那将显示墨尔本的当前时间。

回答by Dylan Markow

Dateobjects don't necessarily have timezones, but Timeobjects do. You can try it as a Time, then convert back to a Date:

Date对象不一定有时区,但Time对象有。您可以尝试将其作为 a Time,然后转换回 a Date

Time.now.to_date
# => Thu, 19 May 2011 
Time.now.in_time_zone('Melbourne').to_date
# => Fri, 20 May 2011 

回答by Josh

Date.current

Date.current

Date.currentis probably the most clear and succinct way, and was added in Rails 3.

Date.current可能是最清晰简洁的方式,并且是在 Rails 3 中添加的。

$ Date.current
#=> Sat, 14 Jul 2018

http://apidock.com/rails/v3.2.13/Date/current/class

http://apidock.com/rails/v3.2.13/Date/current/class

回答by Slick23

It seems Time.zone.todayalso works.

它似乎Time.zone.today也有效。

回答by Tyler Rick

If you want to get "today" in some specified time zone without having to change Time.zone, I would do something like fl00r and Dylan Markow suggested:

如果你想在某个指定的时区获得“今天”而不必更改Time.zone,我会做类似 fl00r 和 Dylan Markow 建议的事情:

Time.now.in_time_zone('Melbourne').to_date

or this:

或这个:

Time.find_zone!('Melbourne').today

I wrote a little helper method Date.today_in_zonethat makes getting a "today" Datefor a time zone even easier:

我编写了一个小助手方法Date.today_in_zone,可以Date更轻松地获取时区的“今天” :

 # Defaults to using Time.zone
 > Date.today_in_zone
=> Fri, 26 Oct 2012

 # Or specify a zone to use
 > Date.today_in_zone('Melbourne')
=> Sat, 27 Oct 2012

I think it reads a little nicer than Time.find_zone!('Melbourne').today...

我觉得它读起来比Time.find_zone!('Melbourne').today……好一点

To use it, just throw this in a file like 'lib/date_extensions.rb'and require 'date_extensions'.

要使用它,只需将其放入像'lib/date_extensions.rb'and 之类的文件中即可require 'date_extensions'

class Date
  def self.today_in_zone(zone = ::Time.zone)
    ::Time.find_zone!(zone).today
  end
end

回答by fl00r

use DateTimeclass

使用DateTime

DateTime.now.in_time_zone 'Melbourne'

回答by mmattke

回答by Max Williams

ruby-1.9.2-p0 :004 > Time.now
 => 2011-05-19 15:46:45 +0100 
ruby-1.9.2-p0 :006 > Time.now.in_time_zone('Melbourne')
 => Fri, 20 May 2011 00:47:00 EST +10:00 
ruby-1.9.2-p0 :007 > Time.now.in_time_zone('Melbourne').to_date
 => Fri, 20 May 2011

回答by Slobodan Kovacevic

In Rails 3 you can simply do this by calling to_time_in_current_zone on a Date object.

在 Rails 3 中,您可以通过在 Date 对象上调用 to_time_in_current_zone 来简单地做到这一点。

Date.today.to_time_in_current_zone