159

In my application.rb I came across the following comment

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
 config.time_zone = 'Eastern Time (US & Canada)'

As you see from above, I've made config.time_zone to EST time. However, still when records are created in the DB, it looks like datetime is being stored in UTC format.

In the above comment, they say

...and make Active Record auto-convert to this zone...

How can I do that, and where?

Also, I'll be deploying this on heroku as well and i'd like the setting to carry over

strivedi183
  • 4,749
  • 2
  • 31
  • 38
Omnipresent
  • 29,434
  • 47
  • 142
  • 186
  • In MySQL datetime is a timezone-less type. I.e. it could be in any timezone you want. If you treat it as UTC that's fine. But beware if somebody's looking into your database directly and interpret it differently. – Vanuan May 04 '16 at 20:25

10 Answers10

244

I have decided to compile this answer because all others seem to be incomplete.

config.active_record.default_timezone determines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :utc. http://guides.rubyonrails.org/configuring.html


If you want to change Rails timezone, but continue to have Active Record save in the database in UTC, use

# application.rb
config.time_zone = 'Eastern Time (US & Canada)'

If you want to change Rails timezone AND have Active Record store times in this timezone, use

# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local

Warning: you really should think twice, even thrice, before saving times in the database in a non-UTC format.

Note
Do not forget to restart your Rails server after modifying application.rb.


Remember that config.active_record.default_timezone can take only two values

  • :local (converts to the timezone defined in config.time_zone)
  • :utc (converts to UTC)

Here's how you can find all available timezones

rake time:zones:all
Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
  • 7
    This seems like the most complete / accurate answer -- @Omnipresent – Aegix Oct 30 '15 at 15:56
  • 3
    This should be the accepted answer. It is more complete. – Enrique Moreno Tent Mar 02 '16 at 13:32
  • @Mihai-AndreiDinculescu Do you think that is better to store the `datetime` values in **UTC** in the database and then just change them in the **frontend** like with [`moment.js`](http://momentjs.com/timezone/) or what do you suggest in order to show this data in the correct time zone for any user? – alexventuraio Nov 11 '16 at 04:20
  • @AlexVentura Yes, people smarter than me agreed a long time ago that storing dates in UTC is the best way to go. – Mihai Dinculescu Nov 11 '16 at 10:29
  • Yeah I got it. But what do you suggest to implement in rails views to show the correct values for any user with different time zone? – alexventuraio Nov 11 '16 at 16:33
  • @AlexVentura You can't unless you get them to tell you their machine's timezone. With a little javascript that shouldn't be hard. Assuming this javascript gives you seconds +/- UTC (e.g. 3600 is +1 hour) you can use it in the controller `before` filter like this: `Time.zone = Time.find_zone! params.fetch(:utc_offset)` – Adamantish Feb 16 '17 at 17:46
  • It may be simpler to just use `moment.js` seeing as javascript is necessary. – Adamantish Feb 16 '17 at 17:49
  • What is `Rails timezone` used for? – Pants Aug 24 '22 at 13:07
198

adding following to application.rb works

 config.time_zone = 'Eastern Time (US & Canada)'
 config.active_record.default_timezone = :local # Or :utc
Wen
  • 1,230
  • 1
  • 14
  • 28
Omnipresent
  • 29,434
  • 47
  • 142
  • 186
  • 61
    It's working for me with this: `config.time_zone = 'London'` `config.active_record.default_timezone = :local` –  May 07 '12 at 23:39
  • 41
    You can see all the Time Zones by running this in `rails c` - `ActiveSupport::TimeZone.all.map(&:name)` –  May 07 '12 at 23:40
  • 5
    Thanks so much! I'd been struggling with this as well. I have an inherited legacy PHP app that interacts with the same mysql database and stores all times as local; updating it to use UTC was not an option. What I had previously accomplished nothing: `config.time_zone = 'Central Time (US & Canada)'` `config.active_record.default_timezone = 'Central Time (US & Canada)'` but setting it to the :local symbol did the trick. – Matt Hucke Jul 30 '12 at 21:53
  • Using `config.active_record.default_timezone = :local` without setting `config.time_zone` worked for me. – Michael Stalker Sep 26 '12 at 17:14
  • 8
    The rails guides say that `config.active_record.default_timezone` should only be set to `:local` or `:utc`. I assume that anything else would have unexpected results. – Kelvin Mar 13 '13 at 16:05
  • @DeanPerry You must make separate answer with your comments! – gotqn Apr 21 '13 at 19:35
  • 2
    You can see all the Time Zones by running this in `rake time:zones:all` – Orlando Aug 07 '13 at 04:31
  • 1
    default_timezone ONLY accept :local or :utc.. anything else will not work.. Yeah it's rather limited in functionality and the name of the var doesn't really portray that well. – Urkle Aug 12 '13 at 15:29
  • _config.active_record.default_timezone_ is deprecated after Rails 3.2.13, and causes problems when used in combination with delayed_job. See: http://stackoverflow.com/questions/22189490/why-is-the-timezone-off-in-delayed-job/22214040#22214040 – joseph.hainline Mar 06 '14 at 03:08
  • Dean Perry's solution worked for me using rails 3.2.14 – tungsten_carbide Apr 18 '14 at 06:40
  • 1
    See all available timezones with `rake:time:all` – Nino van Hooff Jun 26 '15 at 11:06
  • Don't forget to restart your server after you add this lines of codes. – cyonder Sep 30 '15 at 03:31
  • See all available timezones with `rake time:zones` in Rails 5. – traday Sep 05 '17 at 11:08
34

I came to the same conclusion as Dean Perry after much anguish. config.time_zone = 'Adelaide' and config.active_record.default_timezone = :local was the winning combination. Here's what I found during the process.

James Barona
  • 498
  • 4
  • 5
24

In my case (Rails 5), I ended up adding these 2 lines in my app/config/environments/development.rb

config.time_zone = "Melbourne"
config.active_record.default_timezone = :local

That's it! And to make sure that Melbourne was read correctly, I ran the command in my terminal:

bundle exec rake time:zones:all

and Melbourne was listing in the timezone I'm in!

Jeff
  • 6,932
  • 7
  • 42
  • 72
Carole
  • 241
  • 2
  • 2
  • What if I want every user to see date-time values in their own time zone? – alexventuraio Nov 11 '16 at 04:27
  • @AlexVentura when you show date-time values to your users, convert it to their time zones. Making the database records persistent is a better practice – Waheed Mar 03 '19 at 21:28
12

If you want to set the timezone to UTC globally, you can do the following in Rails 4:

# Inside config/application.rb
config.time_zone = "UTC"
config.active_record.default_timezone = :utc

Be sure to restart your application or you won't see the changes.

sergserg
  • 21,716
  • 41
  • 129
  • 182
5

In Ruby on Rails 6.0.1 go to config > locales > application.rb and add the following:

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module CrudRubyOnRails6
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    config.active_record.default_timezone = :local
    config.time_zone = 'Lima'

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

You can see that I am configuring the time zone with 2 lines:

config.active_record.default_timezone =: local
config.time_zone = 'Lima'

I hope it helps those who are working with Ruby on Rails 6.0.1

4

I had to add this block to my environment.rb file and all was well :)

Rails.application.configure do
    config.time_zone = "Pacific Time (US & Canada)"
    config.active_record.default_timezone = :local
end
  • I added it before the line Rails.application.initialize!
Taylor A. Leach
  • 2,115
  • 4
  • 25
  • 43
3

On rails 4.2.2, go to application.rb and use config.time_zone='city' (e.g.:'London' or 'Bucharest' or 'Amsterdam' and so on).

It should work just fine. It worked for me.

Matt
  • 74,352
  • 26
  • 153
  • 180
1

for Chinese user, just add two lines below to you config/application.rb :

config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
Siwei
  • 19,858
  • 7
  • 75
  • 95
-2

If you want local time to set, add the following text in application.rb

config.time_zone = 'Chennai'

# WARNING: This changes the way times are stored in the database (not recommended)
config.active_record.default_timezone = :local

Then restart your server

Anand Rajagopal
  • 1,593
  • 6
  • 24
  • 40
  • 12
    default_timezone accepts one of two options.. :local or :utc.. it doesn't accept a specific time zone, so this will not work.. you need to set default_timezone to :local – Urkle Aug 12 '13 at 15:27