#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 20 comments

1. ChineseGuy May 14, 2007 at 01:31

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


2. jinweijie May 14, 2007 at 01:42

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

I appreciate your greak work! Thanks again!


3. 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


4. 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 :)


5. 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


6. Dima May 14, 2007 at 07:20

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


7. 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.


8. JB May 15, 2007 at 17:10

Very nice show.

Thanks !


9. 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?)


10. 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


11. Nadav May 20, 2007 at 23:54

Great cast.
Thanks


12. 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...


13. 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.


14. 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.


15. 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.


16. 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')


17. pimpmaster Nov 16, 2007 at 04:55

This appears to be broken in Rails 2.0 :*(


18. 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'

:)


19. 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?


20. GregH Apr 17, 2008 at 23:26

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


21. 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.


22. blinton Jan 02, 2009 at 14:03

For Rails 2.2:

# config/initializers/time_formats.rb

Time::DATE_FORMATS[:my_format] = "%B %Y"


23. Reuben S Jan 14, 2009 at 14:25

Great site, thanks for the hard work!
Just a typo probably, but for the record and to encourage proper English usage:
The screen picture above shows the right prepositions (on %B %d at %I %M %p) but the video shows them reversed (at %B %d on %I %M %p).


24. Senyusha Feb 18, 2010 at 07:06

Great site, thanks for the hard work!


25. Lolka Mar 21, 2010 at 03:27

Very nice show.


26. rowing machine Jul 17, 2010 at 00:58

Blogs are good for every one where we get lots of information for any topics. Nice job , keep it up !!!


27. M40310 Jul 25, 2010 at 05:14

Thank you,I think its very useful.


28. free directory list Aug 11, 2010 at 22:18

you for sharing.Nice post


29. 90x workout Aug 12, 2010 at 09:25

I have checked your site and I invite others please check it because the information that is available in your site can spell bound any one.and the images that you have shown in this site also very attractive. There is one more site that I have visited, provide me information that is really unique and the service that they offer also I have experienced, really I enjoy this sevice so I request others please visite this and I want to tell you that after enjoying this service you’ll enjoy it again and again its .


30. cheap air jordans Aug 17, 2010 at 19:43

Thank you for sharing.Nice post!!! hese little tricks are always the best. Thanks for the great screencasts.


31. iPhone repair Aug 23, 2010 at 04:41

It's the difference between a quick format and a full format. The quick format just "zeroes" the volume table whether it's FAT of NTFS. A full format zeroes the data on the drive as well and consequently takes a lot longer.


32. Promotional merchandise Aug 25, 2010 at 23:53

Using the INT( ) function will strip away the decimal value, leaving only the Integer or whole number. Then you can compare the two values from those functions.


33. louis vuitton shoes Aug 26, 2010 at 21:04

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


34. snow boots Aug 31, 2010 at 01:50

 still can't find the documentation for Time class... so imagine if I didn't know...


35. louis vuitton sunglasses Sep 01, 2010 at 21:18

Good article! Thank you so much for sharing this post.Your views truly open my mind.

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
Give Back to Open Source