# Validating a DateTime

**URL:** <https://www.ruby-forum.com/t/validating-a-datetime/93779>\
**Category:** Rails\
**Created:** [April 9, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/validating-a-datetime/93779 "2006-04-09T23:55:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 9, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/validating-a-datetime/93779/1 "2006-04-09T23:55:37Z")

</div>

Hi,

Does anyone know of a method to validate a DateTime field?

I’ve looked at the validation docs and the validates\_date /  
validates\_time plug-in but I can’t find a method to validate a value in  
the format of yyyymmddhhmm. It seems like something that should exist  
already, so I’d just like to check before I attempt my own version…

Cheers,  
Mark

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 10, 2006, 12:26am UTC](https://www.ruby-forum.com/t/validating-a-datetime/93779/2 "2006-04-10T00:26:10Z")

</div>

Hi, please take a look at the parse method at the following site:

[http://www.ruby-doc.org/core/classes/DateTime.html](http://www.ruby-doc.org/core/classes/DateTime.html)

Peace,

-Conrad

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 10, 2006, 12:50am UTC](https://www.ruby-forum.com/t/validating-a-datetime/93779/3 "2006-04-10T00:50:35Z")

</div>

It should be reasonably straight forward to add a validates\_date\_time  
method  
to my plugin. Just adapt the validator and regex code for the kind of  
input  
you want.

[http://svn.viney.net.nz/things/rails/plugins/validates\_date\_time](http://svn.viney.net.nz/things/rails/plugins/validates_date_time)

-Jonathan

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 10, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/validating-a-datetime/93779/4 "2006-04-10T14:52:04Z")

</div>

On 4/10/06, Mark P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a particular reason that validates\_date and validates\_time  
> doesn’t use the above method for Date and Time?
> 
> ie. Date.parse(value\_before\_type\_cast.to\_s) Rescue { errors.add… }

Yes. It guesses wrongly too often when the date is a bit ambiguous. And  
also  
doesn’t support some of the input formats I wanted.

I’ve messed around with the plugin and got it doing something

> approaching what I need, but it’s not very tidy yet.

Email it to me off list if you want. I’ll have a look. I don’t mind  
spending  
a bit of time adding this sort of functionality to the plugin, I’m sure  
others will find it useful and I will probably need it in a month or so  
anyway.

My other problem is now that I have date and time on the form in

> seperate text boxes which are combined to be saved as one DateTime  
> field. Is there a way to validate the input from a form that isn’t a  
> field in the model?

I’m sure we can figure something out. I think if you define {attr} and  
{attr}= methods you can use the validators as normal. Untried though.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 10, 2006, 1:06pm UTC](https://www.ruby-forum.com/t/validating-a-datetime/93779/5 "2006-04-10T13:06:10Z")

</div>

Jonathan V. wrote:

> It should be reasonably straight forward to add a validates\_date\_time  
> method to my plugin. Just adapt the validator and regex code for the  
> kind of input you want.

Conrad T. wrote:

> Hi, please take a look at the parse method at the following site:
> 
> [http://www.ruby-doc.org/core/classes/DateTime.html](http://www.ruby-doc.org/core/classes/DateTime.html)

Thanks for the help…

Is there a particular reason that validates\_date and validates\_time  
doesn’t use the above method for Date and Time?

ie. Date.parse(value\_before\_type\_cast.to\_s) Rescue { errors.add… }

I’ve messed around with the plugin and got it doing something  
approaching what I need, but it’s not very tidy yet.

My other problem is now that I have date and time on the form in  
seperate text boxes which are combined to be saved as one DateTime  
field. Is there a way to validate the input from a form that isn’t a  
field in the model?

Cheers,  
Mark

This message has been checked for viruses but the contents of an  
attachment  
may still contain software viruses, which could damage your computer  
system:  
you are advised to perform your own checks. Email communications with  
the  
University of Nottingham may be monitored as permitted by UK  
legislation.

---

<div class="post-metadata">

**Author:** ![mpaxton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@mpaxton](https://www.ruby-forum.com/u/mpaxton)\
**Post date:** [April 27, 2006, 2:33am UTC](https://www.ruby-forum.com/t/validating-a-datetime/93779/6 "2006-04-27T02:33:12Z")

</div>

Jonathan, does the validates\_time support the :before and :after format?

ie. validates\_time :office\_time, :after =\> “9am”, :before=\>“5pm”

I tried it in the code, and there is no compilation error, but it failed  
my unit tests

- thanks
