Reputation: 3
I have a date returned as a string:
date_str = "2018-08-17"
How would I format this in to another standard date format, eg. 17/08/2018 programatically without having to parse the string manually?
Upvotes: 0
Views: 207
Reputation: 21
Chronic (https://github.com/mojombo/chronic) is a good gem for parsing date/time in any format, including human readable ones.
require 'chronic'
Chronic.parse("2018-08-17")
The nice thing about Chronic is that you can parse even human readable times, like "tomorrow", or "next month".
Chronic.parse('tomorrow')
Upvotes: 0
Reputation: 118271
Use ::strptime
to convert the string to date object, and then convert it to string again with #strftime
method.
require 'date'
Date.strptime('2018-08-17', '%Y-%m-%d').strftime("%d/%m/%Y")
# => "17/08/2018"
Upvotes: 6