Luigi
Luigi

Reputation: 5603

Format date in rails to Year-Month-Date

Using Rails 4.0, I need to return a date for the first of the current month in the following format:

2014-01-01

Date.today.at_beginning_of_month gives me this:

Wed, 01 Jan 2014

How can I format the date to the above?

Upvotes: 0

Views: 426

Answers (2)

Shiva
Shiva

Reputation: 20935

Date.today.at_beginning_of_month.strftime("%F")

See: http://www.ruby-doc.org/core-2.0/Time.html#method-i-strftime for various other date and time formats.

%F => 2007-11-19 Calendar date (extended)

Upvotes: 2

Agis
Agis

Reputation: 33626

Date.today.at_beginning_of_month.strftime # => "2014-01-01"

Upvotes: 4

Related Questions