# Changing Rails Default Date Parse Option

**URL:** <https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069>\
**Category:** Rails\
**Created:** [May 17, 2011, 6:13pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069 "2011-05-17T18:13:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 17, 2011, 6:13pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/1 "2011-05-17T18:13:18Z")

</div>

Does anyone know how to change the default option for the Date.parse  
(which ActiveRecord is using on all date fields). I would like the  
“comp” option to default to true so I don’t have to account for 2-  
digit dates throughout my application.

[http://corelib.rubyonrails.org/classes/Date.html#M001228](http://corelib.rubyonrails.org/classes/Date.html#M001228)

Thanks!  
Tom

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 9:53pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/2 "2011-05-17T21:53:28Z")

</div>

On 17 May 2011 17:11, TomRossi7 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does anyone know how to change the default option for the Date.parse  
> (which ActiveRecord is using on all date fields). I would like the  
> “comp” option to default to true so I don’t have to account for 2-  
> digit dates throughout my application.
> 
> [http://corelib.rubyonrails.org/classes/Date.html#M001228](http://corelib.rubyonrails.org/classes/Date.html#M001228)

Do you mean you are giving ActiveRecord a string and expecting it to  
parse it into a Date as it writes it to the database? If so that is  
generally very dangerous. It is much better to parse it yourself into  
a Date so that you can be sure you are interpreting the input  
correctly, and can cope with parse errors. Then pass the Date to  
ActiveRecord. Or perhaps I am misunderstanding what you are doing.

Colin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 9:59pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/3 "2011-05-17T21:59:01Z")

</div>

ActiveRecord, by default, parses strings using the Date.parse method.  
I’m  
trying to figure out how to tell it to use the comp=true option so it  
will  
parse two-digit years more logically. Currently, the default is false  
which  
allows you to say person.birthday= ‘10/29/75’ and it parses that as  
October  
29th of the year 0075.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 10:38pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/4 "2011-05-17T22:38:36Z")

</div>

On Tuesday, May 17, 2011 3:51:38 PM UTC-4, Colin L. wrote:

> parse it into a Date as it writes it to the database? If so that is  
> generally very dangerous. It is much better to parse it yourself into  
> a Date so that you can be sure you are interpreting the input  
> correctly, and can cope with parse errors. Then pass the Date to  
> ActiveRecord. Or perhaps I am misunderstanding what you are doing.

ActiveRecord typically does a great job in typecasting, handling  
exceptions,  
and validation so I’m not sure what you mean. Though, this is probably  
a  
different discussion altogether since my original question was just how  
to  
tweak the options for the current parsing that ActiveRecord is already  
doing.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 10:41pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/5 "2011-05-17T22:41:05Z")

</div>

What does it matter where the data comes from? ActiveRecord parses it  
the  
same? If you don’t know, or if it isn’t possible, thats fine, but I  
don’t  
want to overcomplicate the question.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 10:23pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/6 "2011-05-17T22:23:46Z")

</div>

On 17 May 2011 20:57, TomRossi7 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ActiveRecord, by default, parses strings using the Date.parse method. I’m  
> trying to figure out how to tell it to use the comp=true option so it will  
> parse two-digit years more logically. Currently, the default is false which  
> allows you to say person.birthday= ‘10/29/75’ and it parses that as October  
> 29th of the year 0075.

Is the above in reply to my questions or are you just asking the same  
question again? If it is in reply to my post then try again, quoting  
my reply and answering the questions.

Another question, where are the date strings coming from? User input?

Don’t forget to answer the questions in the first post though.

Colin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 11:02pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/7 "2011-05-17T23:02:43Z")

</div>

Have you considered using the Chronic gem?

I don’t know how to do what you’re asking but have recently implement  
Chronic to “make date parsing more sane.” (Actual verbiage from my  
assigned task.)

It’s quite a handy gem, you might consider implementing it.

[http://chronic.rubyforge.org/](http://chronic.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 11:15pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/8 "2011-05-17T23:15:07Z")

</div>

On Tue, May 17, 2011 at 4:54 PM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > [http://corelib.rubyonrails.org/classes/Date.html#M001228](http://corelib.rubyonrails.org/classes/Date.html#M001228)
> > > 
> > > Do you mean you are giving ActiveRecord a string and expecting it to  
> > > parse it into a Date as it writes it to the database?
> 
> I am still not sure I understand the question - is the answer to the  
> above question ‘yes’?

The answer is whenever ActiveRecord parses the string. In Rails 3, its  
when  
you call the setter.

> > different discussion altogether since my original question was just how  
> > to  
> > tweak the options for the current parsing that ActiveRecord is already  
> > doing.
> 
> I was just trying to point out that there may be a better way, for  
> example how do you know whether whether ‘10/2/05’ is 2nd October or  
> 10th February? It is generally not a good idea to allow dates to be  
> entered for strings. One man’s date is another man’s exception.

Yeah, in general I agree with you. For my application, Its working  
great  
now, except for when a user uses a 2-digit year.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 17, 2011, 10:55pm UTC](https://www.ruby-forum.com/t/changing-rails-default-date-parse-option/207069/9 "2011-05-17T22:55:55Z")

</div>

On 17 May 2011 21:37, TomRossi7 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > [http://corelib.rubyonrails.org/classes/Date.html#M001228](http://corelib.rubyonrails.org/classes/Date.html#M001228)
> > 
> > Do you mean you are giving ActiveRecord a string and expecting it to  
> > parse it into a Date as it writes it to the database?

I am still not sure I understand the question - is the answer to the  
above question ‘yes’?

> doing.  
> I was just trying to point out that there may be a better way, for  
> example how do you know whether whether ‘10/2/05’ is 2nd October or  
> 10th February? It is generally not a good idea to allow dates to be  
> entered for strings. One man’s date is another man’s exception.

Colin
