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

后端 未结 4 1102
温柔的废话
温柔的废话 2020-12-13 11:56

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

Time.zon         


        
4条回答
  •  不知归路
    2020-12-13 12:37

    Would it be an option for you to use Time.strptime("01/28/2012", "%m/%d/%Y") in place of Time.parse? That way you have better control over how Ruby is going to parse the date.

    If not there are gems: (e.g. ruby-american_date) to make the Ruby 1.9 Time.parse behave like Ruby 1.8.7, but only use it if it's absolutely necessary.

    1.9.3-p0 :002 > Time.parse '01/28/2012'
    ArgumentError: argument out of range
    
    1.9.3-p0 :003 > require 'american_date'
    1.9.3-p0 :004 > Time.parse '01/28/2012'
     => 2012-01-28 00:00:00 +0000 
    

提交回复
热议问题