简体   繁体   中英

how to convert the date format from dd-mmm-yyyy to yyyy-mm-dd in ruby on rails

I am uploading the csv file. The csv file contains the date format in dd-mm-yyyy format. How can I change it to yyyy-mm-dd format. I also tried to_date function but it gives the error as invalid date.

You can do this by below code

require 'date'

date = DateTime.parse("22-02-2019")
formatted_date = date.strftime('%Y-%m-%d')

output

"2019-02-22"

please go through with this document https://www.rubydoc.info/stdlib/core/Time:strftime you will find out the different formatting style

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