Snubber
Snubber

Reputation: 1024

How can I output the day of week with embedded ruby?

Right now I have the date displayed in the format mm/dd/yyyy by using <h6><%= Time.now.strftime("%m/%d/%Y") %></h6> but I want the day of week as well. Preferably it would display like: Wednesday, October 2. Thanks

Upvotes: 0

Views: 422

Answers (3)

Arup Rakshit
Arup Rakshit

Reputation: 118289

Using pure ruby class Date and Time

require 'time'
require 'date'

t = Time.now
t.wday # => 3
Date::ABBR_DAYNAMES[t.wday]
# => "Wed"
Date::DAYNAMES[t.wday]
# => "Wednesday"

Upvotes: 1

Denny Mueller
Denny Mueller

Reputation: 3615

Say i have date = Time.now.to_date then date.strftime("%A") will print name for the day of the week and to have just the number for the day of the week write date.wday.

Copy from here

So Time.now.strftime("%A") and in your case

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

Easiest to test this is the rails c. To remove the leading zero on the %d use %-d instead.

Upvotes: 2

user121489
user121489

Reputation:

You should be able to get the full weekday name with %A (i.e. Sunday) and the abbreviated weekday name with %a (i.e. Sun).

Upvotes: 1

Related Questions