Human Read Date in Rails

I am trying to show the current date in a view in my rails application. I need the following format: Day of the week, Month, Day, Year.

I am currently using:

<%= Time.now.strftime("%B %d, %Y") %>

Everything except the day of the week is displayed here. How to add a day of the week?

+4
source share
3 answers

% A gives the day of the week

Customize as you prefer!

<%= Time.now.strftime("%B %d, %Y, %A") %>
+7
source

You can get the name of the day as follows:

Time.now.strftime("%B %d, %Y %a") # => "January 18, 2014 Sat"
Time.now.strftime("%B %d, %Y %A") # => "January 18, 2014 Saturday"


You can also get day names from a number 0...7using Date::DAYNAMESas follows:

require 'date'
(0...7).each { |x| puts Date::DAYNAMES[x] }
 # Sunday
 # Monday
 # Tuesday
 # Wednesday
 # Thursday
 # Friday
 # Saturday
 # => 0...7
+3
source

%A , . %A , . :

Time.now.strftime("%A, %B %d %Y") # => "Sunday, January 19, 2014"
Time.now.strftime("%a, %B %d %Y") # => "Sun, January 19, 2014"

docs.

+2

All Articles