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'?
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.
Date.today.strftime("%A")
=> "Wednesday"
Date.today.strftime("%A").downcase
=> "wednesday"
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
Time
Object it would be wday
. For example Time.now.wday
Commented
Oct 18, 2017 at 6:43
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.
%A
gives you day of the week.
strftime("%A, %d-%m-%Y")
will give you:
Wednesday, 04-01-2012
Yes, you can use "%A" for the full day name.
See also http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html