Formatting Date in Ruby on Rails

So by default when I used Date.today, the date came out like this 2014/09/05.  However many times you want to display it various formats. This won’t be a comprehensive post on date formatting, but here a few resources that helped me accomplish what I was looking for.

I wanted the date to be formatted like this: Friday, September 5, 2014.  To do this,

Date.today.strftime("%A %B %e %Y ")

You can add commas by simply putting them in the string wherever you want.

Date.today.strftime("%A, %B %e, %Y ")

A very cool website to create your formatting is http://www.foragoodstrftime.com/.  It has an interactive drag and drop code generation tool.

Also some credit here. Other resources include: rails DateTime class, this railscast

Advertisements