In Ruby, how to parse “9/12/2010 7:02pm”?
-
02-10-2019 - |
Question
It seems that Time.parse will treat 9/12/2010 as December 9, 2010:
irb(main):012:0> RUBY_VERSION
=> "1.9.2"
irb(main):013:0> Time.parse('9/12/2010')
=> 2010-12-09 00:00:00 -0800
irb(main):014:0> Time.parse('9/12/2010 7:10pm')
=> 2010-12-09 19:10:00 -0800
I can use Regex to mess with the order and parse accordingly, but is there a different method or gem or any simpler method?
Solution
have you tried
Date.strptime('28/03/2008', '%d/%m/%Y')
DateTime.strptime('28/03/2008 12:30 AM', '%d/%m/%Y %I:%M %p')
updated formatting should work
OTHER TIPS
Using the chronic gem seems to work for me
Time.parse('9/12/2010 7:10pm')
=> Sun Sep 12 19:10:00 -0500 2010
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow