james
james

Reputation: 4047

Rails Time.parse argument out of range

I'm passing a string to a Time.parse but seeing very interesting behavior, see excerpt from console:

2.1.2 :011 > Time.parse("10/10/2014 7 am")
=> 2014-10-10 07:00:00 -0700 

2.1.2 :010 > Time.parse("10/17/2014 7 am")
ArgumentError: argument out of range

What's the difference???

I'm always going to be parsing times that users input in the format of "MM/DD/YYYY H am/pm", so it's crucial that this doesn't break.

Upvotes: 1

Views: 1410

Answers (2)

Kevin Monk
Kevin Monk

Reputation: 1454

Chronic gem is a useful tool for this and you can set the endian_presedence to do what you want.

Upvotes: 0

james
james

Reputation: 4047

Time.parse takes a format of d/m/y, and I couldn't find a way in the docs to change this to m/d/y, so instead, I just changed the input format.

Upvotes: 2

Related Questions