简体   繁体   中英

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

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

You can do this

date = DateTime.now
puts date.to_date.to_s

which gives "2013-08-21"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM