Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: convert string to date

Tags:

string

date

ruby

In Ruby, what's the best way to convert a string of the format: "{ 2009, 4, 15 }" to a Date?

like image 241
jjnevis Avatar asked Apr 27 '10 11:04

jjnevis


People also ask

How do I change the date format in Ruby?

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.

How do you parse a date in Ruby?

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.


1 Answers

You could also use Date.strptime:

Date.strptime("{ 2009, 4, 15 }", "{ %Y, %m, %d }") 
like image 192
robinst Avatar answered Nov 03 '22 19:11

robinst