Rails 3 default datetime format without UTC
I'm creating a new Rails 3 app, and in it I use DateTime
for a couple of fields, however every datetime field standard has UTC behind it (in a view), like:
2010-10-10 16:19:00 UTC
How do I get rid of the UTC part?
UPDATE: here's what I have so far:
<%= trip.truckleft.strftime("%Y-%m-%d %H:%M") %>
So all I have to do now is put that in a helper, but isn't there a better more universal way?
I looked at some other posts, that suggested creating a time_formats.rb
in initializers, however I didn't have any success doing that.
Thanks for your help, much appreciated!
Solution 1:
Another -- perhaps now preferred -- way is to use Rails' internationalization and localization support. There's a lot to learn in that guide, so the tl;dr version is this:
<%= l trip.truckleft, :format => :long %>
There are a few predefined date and time formats like :long
available to you already for English, and you can add your own in config/locales/en.yml
by following the YAML structure in those examples. If you're not getting heavily into the whole i18n/l10n thing just yet and looking at the l
method all the time is confusing, you can also use:
<%= trip.truckleft.to_formatted_s(:long) %>
Solution 2:
Here is what finally worked for me:
I created a new file in:
config/initializers/
named: time_formats.rb
and added this to that file:
Time::DATE_FORMATS[:default] = "%Y-%m-%d %H:%M"
Then I saved, restarted the server and it started to work.
Solution 3:
I'm using i18n to format my dates and have this in en.yml:
date:
formats:
default: "%m/%d/%Y"
I wanted to reuse that format for how the models show their dates, so my config/initializers/time_formats.rb contains this:
Date::DATE_FORMATS[:default] = lambda { |date| I18n.l(date) }