85

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

9 Answers 9

79

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

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

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

6
  • 6
    Time.zone.now.to_date is the most elegant! Thanks! May 19, 2011 at 15:05
  • 26
    Time.current.to_date is even better ! Jun 1, 2011 at 11:49
  • 5
    Even better for any Date object: Date.today.to_time_in_current_zone Jun 22, 2011 at 9:29
  • 2
    @SlobodanKovacevic Date.today.to_time_in_current_zone returns a Time object NOT a Date object.
    – Josh
    Jul 8, 2013 at 16:44
  • 1
    @Ziggy: Time.current.beginning_of_day + 18.hours will work too :)
    – Ryan Bigg
    Aug 13, 2015 at 7:33
45

Date.current

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

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

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

0
20

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
15

It seems Time.zone.today also works.

7

use DateTime class

DateTime.now.in_time_zone 'Melbourne'
2
  • That's a nice, concise way to do it that I hadn't seen before. It looks like Time.now.in_time_zone 'Melbourne' will also work.
    – Tyler Rick
    Oct 26, 2012 at 18:25
  • 1
    This should be upvoted waaay more. Clear, succinct, works with dates, doesn't require ActiveSupport, and doesn't involve any mucking about in config settings. Nov 13, 2013 at 13:44
2
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
2

For valid timezone names, checkout: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

1

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
1
  • 1
    If the server time is different than the Rails time zone, Date.today might return yesterday's or tomorrow's date. Calling to_time_in_current_zone will then make it into a time in the Rails zone, but a time that might be yesterday or tomorrow. This is useful if you have a date known to be in the correct time zone, as from Time.zone.today, but misleading with Date.today.
    – Henrik N
    Feb 4, 2012 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.