7

I keep having to append ".strftime("%H:%M")" to anything that is displaying a time in my Rails project. I have a start time and an end time for each Concert object, so I have to append ".strftime("%H:%M")" whenever I wanna display those times.

Note that I'm not asking to change the date format. The date looks fine as it is (as MM/DD/YYYY).

What's the best way to get around this? Is there a way to set the default time format?

(I'm pretty sure this is only a Ruby thing, but I'm a newbie, so I'm not sure.)

boo-urns
  • 10,136
  • 26
  • 71
  • 107
  • Allow me to suggest not using MM/DD/YYYY for your date format unless your site is very clearly only of use to people in the US. It's often ambiguous (e.g. 01/02/2011) and much of the rest of the world uses DDMMYYYY. Better to use a (possibly short) name for the month. – noodl Jan 12 '11 at 23:57
  • Noted! I personally use DD/MM/YYYY myself despite being in the States, but this project is for a very, very small party, all of whom are from the US and use the MM/DD convention. – boo-urns Jan 13 '11 at 00:22

4 Answers4

17

Since you're using Rails, take advantage of the I18n support: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats

# config/locales/en.yml
en:
  time:
    formats:
      default: "%H:%M"

Then when you call I18n.l Time.now or <%= l Time.now %>, it'll use that format automatically.

coreyward
  • 77,547
  • 20
  • 137
  • 166
  • So, I might be missing something, but I've added that to the yml file, and have yet to see a change. I'm running Rails 2.3.8, and I remember having trouble with internationalization before with the Agile book. The only way I can get some translations is using the t method. From what I've read, the translations should be automatic. What might I be doing wrong? – boo-urns Jan 13 '11 at 00:25
  • This might seem silly, but have you restarted the server? I don't think the translation files are reloaded automatically. If so, double check the syntax. YAML is very particular about the indentation. Finally, try specifying the "default" explicitly to make sure that the docs aren't incorrect in this behavior. – coreyward Jan 13 '11 at 01:36
  • Hmm, I did try restarting the server, but that doesn't seem the be the problem. I'm not sure what you mean by specifying the default explicitly, though... – boo-urns Jan 14 '11 at 19:37
2

Ruby on Rails has built-in presets for formatting Date and Time instances. Here's what they are for Time on my machine:

>> Time::DATE_FORMATS
=> {:short=>"%d %b %H:%M", :db=>"%Y-%m-%d %H:%M:%S", :rfc822=>#<Proc:0x0000000103700b08@/Users/donovan/.gem/gems/activesupport-3.0.1/lib/active_support/core_ext/time/conversions.rb:13>, :time=>"%H:%M", :number=>"%Y%m%d%H%M%S", :long_ordinal=>#<Proc:0x0000000103700e50@/Users/donovan/.gem/gems/activesupport-3.0.1/lib/active_support/core_ext/time/conversions.rb:12>, :long=>"%B %d, %Y %H:%M"}

You can easily use them like so:

>> Time.now.to_s(:db)
=> "2011-01-12 15:26:11"

You can define your own and use it, too:

>> Time::DATE_FORMATS[:mine] = "%H:%M"
=> "%H:%M"
>> Time.now.to_s(:mine)
=> "15:28"
Brian Donovan
  • 8,274
  • 1
  • 26
  • 25
  • Where should I put "Time::DATE_FORMATS[:mine] = "%H:%M""? What version of Ruby/Rails are you using? I get "NameError: uninitialized constant Time::DATE_FORMATS" in irb when I try that. – boo-urns Jan 12 '11 at 23:34
  • I'm using Rails 3.0.1 in the app I tested it in. You should take @coreyward's suggestion to add it to the locale config. – Brian Donovan Jan 12 '11 at 23:35
1

You could create a custom function in your application_helper.rb file. Maybe something like:

def custom_time(date)
  date.strftime("%H:%M")
end

Not sure if this is best practice but it should work well.

JackCA
  • 4,885
  • 4
  • 27
  • 26
0

I use an initializer: config/initializers/time_formats.rb which contains:

[Time, Date].map do |klass|
  klass::DATE_FORMATS[:app_date] = "%m/%d/%Y"
  klass::DATE_FORMATS[:app_month_and_year] = "%B %Y"
  klass::DATE_FORMATS[:app_abbrev_month_and_year] = "%b %Y"
  ...
end

Then I use Time.now.to_s(:app_date), etc based on how I want to display it. You could add Time::DATE_FORMATS[:default] = "%H:%M" to accomplish what you're trying to do. This is not internationalized though - for that, coreyward's answer is probably better.

Brian Deterling
  • 13,556
  • 4
  • 55
  • 59