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 \'Melbour
It seems Time.zone.today
also works.
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.now.in_time_zone('Melbourne').to_date
or this:
Time.find_zone!('Melbourne').today
I wrote a little helper method Date.today_in_zone
that makes getting a "today" Date
for a time zone even easier:
# 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
...
To use it, just throw this in a file like '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
In Rails 3 you can simply do this by calling to_time_in_current_zone on a Date object.
Date.today.to_time_in_current_zone