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

后端 未结 4 1098
温柔的废话
温柔的废话 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:30

    As above, use the strptime method, but note the differences below

    Date.strptime("12/22/2011", "%m/%d/%Y") => Thu, 22 Dec 2011
    DateTime.strptime("12/22/2011", "%m/%d/%Y") => Thu, 22 Dec 2011 00:00:00 +0000
    Time.strptime("12/22/2011", "%m/%d/%Y") => 2011-12-22 00:00:00 +0000 
    

    (the +0000 is the timezone info, and I'm now in GMT - hence +0000. Last week, before the clocks went back, I was in BST +0100. My application.rb contains the line config.time_zone = 'London')

提交回复
热议问题