Finnnn
Finnnn

Reputation: 3590

How do I get day of the week from a date object?

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'?

Upvotes: 22

Views: 24008

Answers (6)

Winston Kotzan
Winston Kotzan

Reputation: 2087

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

Upvotes: 7

abhilashak
abhilashak

Reputation: 3621

Date.today.strftime("%A")
=> "Wednesday"

Date.today.strftime("%A").downcase
=> "wednesday"

Upvotes: 15

Syed Aslam
Syed Aslam

Reputation: 8807

%A gives you day of the week.

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

Wednesday, 04-01-2012

Upvotes: 4

Tim Brandes
Tim Brandes

Reputation: 2213

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

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

Upvotes: 2

Alex
Alex

Reputation: 9481

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.

Upvotes: 24

0x4a6f4672
0x4a6f4672

Reputation: 28245

try %A instead of %w

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

Upvotes: 1

Related Questions