Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby DateTime parsing from 'mm/dd/yyyy' format

I am using ruby 1.9.3 and want to get Date or Time object from 'mm/dd/yyyy' date format string

Time.zone.parse("12/22/2011") 

this is giving me *** ArgumentError Exception: argument out of range

like image 667
TKumar Stpl Avatar asked Oct 28 '13 19:10

TKumar Stpl


People also ask

How do you parse time 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.

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.


1 Answers

require 'Date' my_date = Date.strptime("12/22/2011", "%m/%d/%Y") 
like image 131
hirolau Avatar answered Oct 18 '22 10:10

hirolau