# Date#parse assume a EU format - can it assume a US format?

**URL:** <https://www.ruby-forum.com/t/date-parse-assume-a-eu-format-can-it-assume-a-us-format/195282>\
**Category:** Ruby\
**Created:** [September 21, 2010, 3:50am UTC](https://www.ruby-forum.com/t/date-parse-assume-a-eu-format-can-it-assume-a-us-format/195282 "2010-09-21T03:50:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [September 21, 2010, 3:50am UTC](https://www.ruby-forum.com/t/date-parse-assume-a-eu-format-can-it-assume-a-us-format/195282/1 "2010-09-21T03:50:50Z")

</div>

Before you jump to an answer here, please don’t suggest that I use  
\_strptime. Going that route forces my users to give me the date and/or  
time in the format that I specify. Which is really not optimal.

So the problem is that Date#parse assumes that the format is  
day/month/year. I would like to make it assume that it is in the US  
format, month/day/year.

After digging into format.rb I tried this:

class Date  
def \_parse\_eu(str, e)  
\_parse\_us(str,e)  
end  
end

… in an attempt to skip the parse\_eu method… that solution doesn’t  
exactly work:

ruby-1.9.2-p0 \> Date.parse(“10/04/2010”)  
=\> Sat, 10 Apr 2010

So, it’s still using the EU format. Any other suggestions??

Thanks!

---

<div class="post-metadata">

**Author:** ![joshs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshs](https://www.ruby-forum.com/u/joshs)\
**Post date:** [September 21, 2010, 11:15am UTC](https://www.ruby-forum.com/t/date-parse-assume-a-eu-format-can-it-assume-a-us-format/195282/2 "2010-09-21T11:15:37Z")

</div>

Josh Sharpe wrote:

> Before you jump to an answer here, please don’t suggest that I use  
> \_strptime. Going that route forces my users to give me the date and/or  
> time in the format that I specify. Which is really not optimal.
> 
> So the problem is that Date#parse assumes that the format is  
> day/month/year.

It is U.S. middle-endian format in 1.8.7. But try this monkey-patch for  
1.9.2:

require ‘date’  
def Date.\_parse\_sla(str, e) # :nodoc:  
if str.sub!(%r|(’?-?\d+)/\s\*(’?\d+)(?:\D\s\*(’?-?\d+))?|, ’ ') # ’  
s3e(e, $2, $1, $3)  
true  
end  
end

> > Date.parse(“10/04/2010”)  
> > =\> #\<Date: 2010-10-04 (4910947/2,0,2299161)\>
