简体   繁体   English

Rails将包含日期时间的字符串转换为日期

[英]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. 我有一个字符串,其中包含以下字符串2012-03-06 00:00:00 UTC我想将其更改为日期对象,因此应该看起来像这个03-06-2012 ,同样是在字符串中转换03-06-2012以便jquery 03-06-2012可以接受它。

All this conversion needs to be done at view .html.erb 所有这些转换都需要在查看.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" 给出了“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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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