#31
May 14, 2007

Formatting Time

Learn how to use the trusty strftime method to format a time, and see how Rails allows you to save this format for later use.
Download (17.9 MB, 5:01)
alternative download for iPod & Apple TV (7.3 MB, 5:01)
# config/environment.rb
Time::DATE_FORMATS[:due_time] = "due on %B %d at %I:%M %p"
<%= task.due_at.to_s(:due_time) %>

RSS Feed for Episode Comments 25 comments

1. weskycn May 14, 2007 at 00:38

非常实用,我喜欢,这些技巧解决实际问题!
very well ,thanks.
go on ..
ChineseGuy , come。


2. ChineseGuy May 14, 2007 at 01:31

I'am here :)
我在这儿 :)


3. jinweijie May 14, 2007 at 01:42

Thank you! This really what i'm looking for!

I appreciate your greak work! Thanks again!


4. Rebort May 14, 2007 at 02:27

Great tips.

Uhm .. how did you get that command line "ri" documentation to work? The command exists on my system but it looks like the actual docs are missing, eg:

$ ri Time.strftime
Nothing known about Time.strftime


5. eskim May 14, 2007 at 03:08

Is there any way to put the format string things in the file other than environment.rb?

Thanks for the great screencasts.
I'm big fan :)


6. Christoph Olszowka May 14, 2007 at 03:52

Hi, thanks for the hint with Time::DATE_FORMATS, I've been looking for quite a while for something like this :)

You can push this even further be adding a alias_method for to_s in Time class like this:
class Time
  alias_method :[], :to_s
end

Now you can do something like Time.now[:custom_format] etc.

There is no []-instance method for the time-class, so this does not destroy some other behaviour in your existing app.

Greetings, Christoph


7. Dima May 14, 2007 at 07:20

I appreciate your greak work!
These little tricks are always the best.
Thanks for the great screencasts.


8. Ryan Bates May 14, 2007 at 07:27

@Rebort, hmm, how did you install Ruby? Did you compile it from the source? If so, you needed to do "make install-doc" command after "make install".

@eskim, yep, you can move this into a file in the "lib" directory. Then just "require" the file in your environment.rb.


9. weskycn May 14, 2007 at 17:53

恩,好!!


10. weskycn May 14, 2007 at 17:54

一定要报到阿


11. JB May 15, 2007 at 17:10

Very nice show.

Thanks !


12. Zubin May 16, 2007 at 10:02

Thanks Ryan, another great screencast!

If anyone's wondering, %e = Day of the month with no leading zeros (0-31). This is missing in "ri strftime" for both Time and Date ("ri Date.strftime" returned no info for me).

Does anyone know how to add a more generic string formatting function to the environment file (or elsewhere), which is available to models, controllers and views? (eg str_to_webname?)


13. Ryan Bates May 16, 2007 at 11:47

@Zubin, cool, didn't know about %e.

Regarding your question, you could extend the String class to add your own method which does this. Place this in a file in your lib directory and require it in environment.rb

--
class String
  def to_webname
    #...
  end
end
--

sorry there's no indentations


14. Nadav May 20, 2007 at 23:54

Great cast.
Thanks


15. Tulsi May 21, 2007 at 06:45

absolutely useful tip... even now that I know it, I still can't find the documentation for Time class... so imagine if I didn't know...


16. schlick Jul 10, 2007 at 07:10

Nifty piece of advice and I have already implemented it in my project however it seems to cause my functional tests to fail. Is there anything extra/special that needs to be done in other for it to work in my tests. I keep getting a 'wrong number of parameters' error for to_s.


17. Ryan Bates Jul 10, 2007 at 07:28

@schlick, make sure each of your fixtures has the time column specified in it. It sounds like the time is returning "nil" which will result in the error you are receiving. If you still can't get it to work I recommend you post on railsforum.com about this.


18. schlick Jul 11, 2007 at 06:19

Thanks Ryan for the quick reply. And yes, you were right. I needed to add in values for my datetimes into my fixtures.


19. IceskYsl Sep 24, 2007 at 01:34

very cool~
Add This:
  ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:my_format => '%y-%m-%d %H:%M')


20. pimpmaster Nov 16, 2007 at 04:55

This appears to be broken in Rails 2.0 :*(


21. pimpmaster Nov 16, 2007 at 05:15

For Rails 2.0 you need to do like this in your config:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS[:due_on] = '%m-%d-%y'

:)


22. akatako Feb 08, 2008 at 01:35

With rails 2.0.2 and after adding ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS[:rfc3339] = '%Y-%m-%dT%H:%M:00.000Z' to environment.rb I get a "rake aborted! uninitialized constant ActiveSupport" each time I run rake db:migrate. I am unable to find why... someone can help?


23. GregH Apr 17, 2008 at 23:26

I get this error too. Anyone know how to avoid this?


24. Abey Mullassery Apr 27, 2008 at 22:09

GregH, akatako,

Try adding those after the

Rails::Initializer.run do |config|
..
end

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
...
)

Hope that helps.
I had a similar issue with ActionMailer as well.


25. kino May 04, 2008 at 03:32

非常实用,我喜欢,这些技巧解决实际问题!
 

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player