48

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!

Jason Meckley
  • 7,589
  • 1
  • 24
  • 45
Naxels
  • 1,725
  • 2
  • 14
  • 19

6 Answers6

66

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) %>
georgebrock
  • 28,393
  • 13
  • 77
  • 72
ches
  • 6,382
  • 2
  • 35
  • 32
  • 5
    Argh, here is [the guide I was trying to link](http://guides.rubyonrails.org/i18n.html#adding-date-time-formats). Stack Overflow wouldn't let me post more than one link because my account is new. Sheesh. – ches Dec 10 '10 at 22:07
  • +1 and upvoted to get your rep higher--Stack Overflow needs more people like you – jbnunn Mar 16 '12 at 22:21
42

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.

Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
Naxels
  • 1,725
  • 2
  • 14
  • 19
  • This works for me on 4.1. The advantage of this method is that you often don't need to even add the to_s, most contexts will turn the time into a string automatically. You can also add other formats such as Time::DATE_FORMATS[:date_only] = "%Y-%m-%d" and then use them like so date.to_s(:date_only) - less code than the internationalised version, therefore better if you have no plans to internationalise. – Paul Odeon Mar 23 '14 at 18:37
  • You just have to add Date::DATE_FORMATS[:default] = "%Y-%m-%d %H:%M" as well, not only the time format ;) Works for me in rails > 4.1 – schlenger May 17 '15 at 20:00
26

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) }
jankubr
  • 471
  • 4
  • 5
14

To be exact, you should put these in your initializers:

Date::DATE_FORMATS[:default] = "%m-%d-%Y"
Time::DATE_FORMATS[:default] = "%m-%d-%Y %H:%M"

When having datetime, the second one will work (for example: created_at for in models).

1

for rails 3

add to config/environment.rb

my_datetime_formats = { :default => '%F %T' } #or any other you like
my_date_formats = { :default => '%F' } #or any other you like

Time::DATE_FORMATS.merge!(my_datetime_formats)
Date::DATE_FORMATS.merge!(my_date_formats)

(the difference from other answers - is merge! method)

okliv
  • 3,909
  • 30
  • 47
1

You can put the following line at the end of your config/environment.rb file:

Date::DATE_FORMATS[:default] = "%Y-%m-%d %H:%M"
Daniel O'Hara
  • 13,307
  • 3
  • 46
  • 68