简体   繁体   中英

String to Date in RoR

Hi I have this code in RoR to convert String into a date:

r.valid_from = Date.strptime(row[5], @time_format)

row[5] is 7-11-12 and time_format is "%d-%m-%y". The result I get with this code is: November 07, 2012 but I would like to get it in this way: 07 November, 2012. How can I do that? Thanks!

You just need to format the time:

DateTime.strptime(row[5], @time_format).strftime("%d %B, %Y")

See the documentation on strftime for all the formatting directives.

It seems you're successfully parse the string to the Date . So you just need to print your new date in another format.

To achieve 07 November, 2012 result, you can use strftime :

your_date.strftime('%d %B %Y')

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