22

I have a date object in Rails, which I'd like to format.

I've made it this far:

delivery_time.date.strftime("%w, %d/%m/%Y")

I'd like it to print out 'Wednesday, 04/01/2012'

Is there a quick method to change the '%w' into 'Wednesday'?

6 Answers 6

24

Looking at the Ruby docs for strftime

Time.now.strftime("%A, %d/%m/%Y")
=> "Wednesday, 04/01/2012"

The %A character is the full day name.

1
  • Thanks for the link to the docs too. I should try remember to check that before I go for random google searches.
    – Finnnn
    Commented Jan 4, 2012 at 11:32
15
Date.today.strftime("%A")
=> "Wednesday"

Date.today.strftime("%A").downcase
=> "wednesday"
1
  • 2
    You can also use Date::DAYNAMES[Date.today.cwday].
    – BrunoF
    Commented Jan 15, 2019 at 17:01
7

I know the OP wanted the string for the weekday, but if for whatever reason you are doing something more algorithmic and need a numerical representation, you can use cwday on the Date object

d = Date.new(2017,7,26) 
d.cwday
#=> 3

Monday is 1

2
  • 1
    For the Time Object it would be wday. For example Time.now.wday
    – Marco Roth
    Commented Oct 18, 2017 at 6:43
  • For both Date and Time, wday returns the weekday in the range 0..6, where 0=Sunday. For Date, cwday returns the weekday in the range 1..7, where 1=Monday.
    – ehymel
    Commented Nov 2, 2017 at 17:51
4

%A gives you day of the week.

strftime("%A, %d-%m-%Y") will give you:

Wednesday, 04-01-2012
2

Yes, you can use "%A" for the full day name.

See also http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html

1

try %A instead of %w

delivery_time.date.strftime("%A, %d/%m/%Y")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.