views:

54

answers:

2

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?

+1  A: 

Using the chronic gem seems to work for me

Time.parse('9/12/2010 7:10pm')    
 => Sun Sep 12 19:10:00 -0500 2010 
Chubas
Do you mean `Chronic.parse` instead of `Time.parse`?
動靜能量
+1 for giving a good parsing solution... that works well if the format changes slightly.
動靜能量
+6  A: 

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

Luke Schafer
+1 for using a built-in function rather than resorting to a gem.
sosborn