15

How can I get the name of the day in Ruby?

For eg. something like that:

a = Date.today
a.day_name
# => Tuesday

the only thing I could find was .wday but that only returns for the number of the day in the week

a.wday
# => 2
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
John Smith
  • 6,105
  • 16
  • 58
  • 109

2 Answers2

35
Time.now.strftime("%A")
# => "Tuesday"

or

Date.today.strftime("%A")
# => "Tuesday"
Weekday:
  %A - The full weekday name (``Sunday'')
          %^A  uppercased (``SUNDAY'')
  %a - The abbreviated name (``Sun'')
          %^a  uppercased (``SUN'')

Source: strftime @ apidock.com

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
shivam
  • 16,048
  • 3
  • 56
  • 71
11

Since the OP references Date.today, we can assume the presence of Date (Rails, not pure Ruby), so a simpler solution would be to use the Date::DAYNAMES array:

Date::DAYNAMES[Date.today.wday]
Daniel
  • 794
  • 1
  • 8
  • 15