Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 547
Paul Phoenix Avatar asked Aug 21 '13 14:08

Paul Phoenix


3 Answers

Date.parse("2012-03-06 00:00:00 UTC").strftime("%d-%m-%Y")
like image 113
Alive Developer Avatar answered Nov 10 '22 11:11

Alive Developer


You can do this

date = DateTime.now
puts date.to_date.to_s

which gives "2013-08-21"

like image 27
Althaf Hameez Avatar answered Nov 10 '22 11:11

Althaf Hameez


date = Date.parse('2012-03-06 00:00:00 UTC')
# => Tue, 06 Mar 2012
date.strftime('%d-%m-%Y')
# => "06-03-2012"
like image 44
Marek Lipka Avatar answered Nov 10 '22 11:11

Marek Lipka