# Date validation

**URL:** <https://www.ruby-forum.com/t/date-validation/177410>\
**Category:** Rails\
**Created:** [October 16, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/date-validation/177410 "2009-10-16T16:44:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 16, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/date-validation/177410/1 "2009-10-16T16:44:30Z")

</div>

Hello,

What is the best way to validate date periods? I have a model which has  
two date attributes, and the user is not able to create a new record if  
date1 is greater than date2. I tried using validates\_numericality\_of but  
it doesn’t work with dates.

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 16, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/date-validation/177410/2 "2009-10-16T17:49:06Z")

</div>

Eduardo B. wrote:

> ```
> Hello,
> 
> What is the best way to validate date periods? I have a model which has  
> two date attributes, and the user is not able to create a new record if  
> date1 is greater than date2. I tried using validates\_numericality\_of but  
> it doesn’t work with dates.
> ```

I created my own validator for dates checking for the correct order and for valid dates.Â I check valid dates by creating and catchng the exception if the date is invalid.  
  
  
  
--~--~---------~--~----~------------~-------~--~----~  
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.   
 To post to this group, send email to removed\_email\_address@domain.invalid   
 To unsubscribe from this group, send email to removed\_email\_address@domain.invalid   
 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en  
-~----------~----~----~----~------~----~------~--~---

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 16, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/date-validation/177410/3 "2009-10-16T18:25:58Z")

</div>

Create your own validation method like this:

[http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html#creating-custom-validation-methods](http://guides.rubyonrails.org/activerecord_validations_callbacks.html#creating-custom-validation-methods)

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [October 17, 2009, 12:58am UTC](https://www.ruby-forum.com/t/date-validation/177410/4 "2009-10-17T00:58:46Z")

</div>

irb(main):001:0\> require ‘date’  
=\> true

irb(main):002:0\> d1 = Date.new(2009, 4, 1)  
=\> #\<Date: 2009-04-01 (4909845/2,0,2299161)\>

irb(main):003:0\> d2 = Date.new(2009, 5, 1)  
=\> #\<Date: 2009-05-01 (4909905/2,0,2299161)\>

irb(main):004:0\> d3 = Date.new(2009, 6, 1)  
=\> #\<Date: 2009-06-01 (4909967/2,0,2299161)\>

irb(main):005:0\> d2.between? d1, d3  
=\> true

On Oct 16, 12:24 pm, “removed\_email\_address@domain.invalid”
