In Ruby, what's the best way to convert a string of the format: "{ 2009, 4, 15 }"
to a Date?
Two steps: You need to convert your string into Date object. For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.
Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.
You could also use Date.strptime
:
Date.strptime("{ 2009, 4, 15 }", "{ %Y, %m, %d }")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With