Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Apr 11 ’11

Format a string from a Time object

If you only want to print the first day of the current month you can use the string formating method on a Time object:

> Time.now.strftime("%Y-%m-1")
"2011-04-1"

The method can be used to print date in almost any format:

> Time.now.strftime("%A %B %d or %a %e/%m")
=> "Monday April 11 or Mon 11/04"

Check out Time#strftime for a list of directives. Time is part of the core, so there is no need to require anything.

This tip was submitted by Aqab Bin Talal.

0 comments

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus