Reputation: 3
Is there a way in rails with localize or in plain ruby with Date.strptime to convert a string into a date but with a specific months locales?
For example, I have the following string -> '5 abr 19'
where abr == april
but in spanish, and I need it to convert it to a Date object.
Upvotes: 0
Views: 315
Reputation: 15838
If you have the date month in english, you can use Date.strptime with this format:
date_str = '5 apr 19'
date = Date.strptime(date_str, '%d %b %y')
Since that method only accepts english I would just translate the month, only a few months changes:
date_str = '5 abr 19'
months = {'ene' => 'jan', 'abr' => 'apr', 'ago' => 'aug', 'dic' => 'dec'}
date_str_aux = date_str.gsub(/ene|abr|ago|dic/, months) # only replace the months that should be translated
date = Date.strptime(date_str_aux, '%d %b %y')
Upvotes: 1