Paul Phoenix
Paul Phoenix

Reputation: 1433

Rails convert string containing datetime to date

I have a String which contains the following string 2012-03-06 00:00:00 UTC I want to change it into a date object so that is should look like this 03-06-2012 and the same is to be converted in String 03-06-2012 so that the jquery datepicker can take it.

All this conversion needs to be done at view .html.erb

Upvotes: 7

Views: 12994

Answers (3)

Marek Lipka
Marek Lipka

Reputation: 51151

date = Date.parse('2012-03-06 00:00:00 UTC')
# => Tue, 06 Mar 2012
date.strftime('%d-%m-%Y')
# => "06-03-2012"

Upvotes: 2

Alive Developer
Alive Developer

Reputation: 1022

Date.parse("2012-03-06 00:00:00 UTC").strftime("%d-%m-%Y")

Upvotes: 6

Althaf Hameez
Althaf Hameez

Reputation: 1511

You can do this

date = DateTime.now
puts date.to_date.to_s

which gives "2013-08-21"

Upvotes: 3

Related Questions