I am importing data from CSV inside Rails 3.2 and saving it to mongodb collection and everything works fine except the date field. The imported date format is DD/MM/YYY. Please how can I convert the imported date to YYYY-MM-DD?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use date parsing like this:
Date.strptimecreates aDateobject from a string in the given formatDate#to_sreturns it in the ISO 8601 format (i.e. YYYY-MM-DD)But it depends on how big your CSV is – this would create a bunch of intermediate
Dateobjects which would be a bit slower than a (slightly ugly) string indexing approach:Update
I was curious so I ran some quick benchmarks – the date parsing method was about 2.7 times slower than the string method (5.289s vs 1.981s for a million conversions, Ruby 1.9.3/Windows). YMMV.