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

One Response to Formatting Date in Ruby on Rails

  1. Pingback: Create a PDF with Prawn | How I Learned Ruby on Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: