# Date & time validation

**URL:** <https://rubytalk.org/t/date-time-validation/55172>\
**Category:** ruby-talk\
**Created:** [1 September 2009 13:04 UTC](https://rubytalk.org/t/date-time-validation/55172 "2009-09-01T13:04:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ne\_Scripter](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@Ne\_Scripter](https://rubytalk.org/u/Ne_Scripter)\
**Post date:** [1 September 2009 13:04 UTC](https://rubytalk.org/t/date-time-validation/55172/1 "2009-09-01T13:04:48Z")

</div>

Hi all,

I am looking for an easy way to validate date and time values in a  
script I have written.

The script takes values from a large file containing various data sets,  
some dates and some times. I need to validate these dates and time  
values once I have loaded them into a variable and I was thinking of  
doing this through an a date check and time check object for example:

date1 = field[2]  
date1 = dateCheck(date1) if date1 != ""

date2 = field[23]  
date2 = dateCheck(date2) if date2 != ""

time = field[24]  
time = timeCheck(time) if time != ""

def dateCheck(dateData)  
&nbsp;&nbsp;I need some code to check what ever is in this string and check for a  
valid date of yyyy/mm/dd. The data being checked can be anything, so  
I need to know does it match the date format of yyyy/mm/dd, if so return  
true otherwise return an empty string  
end

def timeCheck(timeData)  
&nbsp;&nbsp;similar idea with the time I need to check the data is hh:mm:ss  
otherwise return an empty string.  
end

Any ideas on how to do this? I was looking a the docs for date and I am  
struggling to make head way at this stage.

Thanks in advance!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [1 September 2009 13:33 UTC](https://rubytalk.org/t/date-time-validation/55172/2 "2009-09-01T13:33:34Z")

</div>

Just parse it into a Time instance:

15:32:29 ~$ irb19 -r time  
Ruby version 1.9.1  
irb(main):001:0\> Time.parse '2009/01/02'  
=\> 2009-01-02 00:00:00 +0100  
irb(main):002:0\> Time.parse '2009/20/02'  
ArgumentError: argument out of range  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/time.rb:202:in `local'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/time.rb:202:in `make\_time'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/time.rb:261:in `parse'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/bin/irb19:12:in `\<main\>'  
irb(main):003:0\>

Cheers

robert

> **···**
>
> 2009/9/1 Ne Scripter \<stuart.clarke@northumbria.ac.uk\>:
> 
> > Hi all,
> > 
> > I am looking for an easy way to validate date and time values in a  
> > script I have written.
> > 
> > The script takes values from a large file containing various data sets,  
> > some dates and some times. I need to validate these dates and time  
> > values once I have loaded them into a variable and I was thinking of  
> > doing this through an a date check and time check object for example:
> > 
> > date1 = field[2]  
> > date1 = dateCheck(date1) if date1 != ""
> > 
> > date2 = field[23]  
> > date2 = dateCheck(date2) if date2 != ""
> > 
> > time = field[24]  
> > time = timeCheck(time) if time != ""
> > 
> > def dateCheck(dateData)  
> > I need some code to check what ever is in this string and check for a  
> > valid date of yyyy/mm/dd. The data being checked can be anything, so  
> > I need to know does it match the date format of yyyy/mm/dd, if so return  
> > true otherwise return an empty string  
> > end
> > 
> > def timeCheck(timeData)  
> > similar idea with the time I need to check the data is hh:mm:ss  
> > otherwise return an empty string.  
> > end
> > 
> > Any ideas on how to do this? I was looking a the docs for date and I am  
> > struggling to make head way at this stage.
> > 
> > Thanks in advance!
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Ne\_Scripter](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@Ne\_Scripter](https://rubytalk.org/u/Ne_Scripter)\
**Post date:** [1 September 2009 14:00 UTC](https://rubytalk.org/t/date-time-validation/55172/3 "2009-09-01T14:00:37Z")

</div>

Robert,

I understand what you are doing here and I have looked at using this  
already however if I have a string 2009/09/01 12:22:36 it will be  
returned as true because the date format is correct, the string above  
will in fact be invalid because only a field consisting of yyyy/mm/dd is  
valid.

Many thanks

Stuart

Robert Klemme wrote:

> **···**
>
> > 2009/9/1 Ne Scripter \<stuart.clarke@northumbria.ac.uk\>:
> > 
> > > date1 = field[2]  
> > > valid � �date of yyyy/mm/dd. The data being checked can be anything, so
> > > 
> > > Any ideas on how to do this? I was looking a the docs for date and I am  
> > > struggling to make head way at this stage.
> > > 
> > > Thanks in advance!
> > 
> > Just parse it into a Time instance:
> > 
> > 15:32:29 ~$ irb19 -r time  
> > Ruby version 1.9.1  
> > irb(main):001:0\> Time.parse '2009/01/02'  
> > =\> 2009-01-02 00:00:00 +0100  
> > irb(main):002:0\> Time.parse '2009/20/02'  
> > ArgumentError: argument out of range  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/time.rb:202:in `local'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/time.rb:202:in `make\_time'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/time.rb:261:in `parse'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/bin/irb19:12:in `\<main\>'  
> > irb(main):003:0\>
> > 
> > Cheers
> > 
> > robert
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Max\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Max\_Williams](https://rubytalk.org/u/Max_Williams)\
**Post date:** [1 September 2009 14:14 UTC](https://rubytalk.org/t/date-time-validation/55172/4 "2009-09-01T14:14:11Z")

</div>

You could run it through a rough regex match first and then Date parse  
it using strptime.

date = (string =~ /^\d{4}\/\d{2}\/\d{2}$/) &&  
(Date.strptime(string,"%Y/%m/%d"))

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [1 September 2009 14:27 UTC](https://rubytalk.org/t/date-time-validation/55172/5 "2009-09-01T14:27:08Z")

</div>

Max Williams wrote:

> date = (string =~ /^\d{4}\/\d{2}\/\d{2}$/) &&

Beware: use

date = (string =~ /\A\d{4}\/\d{2}\/\d{2}\z/) ...

or else malicious inputs may be able to bypass your validation. (They  
might not be able to bypass Date.strptime in this particular instance,  
but still you ought to validate correctly)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [1 September 2009 14:51 UTC](https://rubytalk.org/t/date-time-validation/55172/6 "2009-09-01T14:51:17Z")

</div>

Then you can do:

16:48:53 ~$ irb19 -r date  
Ruby version 1.9.1  
irb(main):001:0\> %w{2009/10/11 2009/10/44}.each do |s|  
irb(main):002:1\* p s  
irb(main):003:1\> date = %r{\A(\d{4})/(\d{2})/(\d{2})\z} =~ s &&  
Date.new($1.to\_i, $2.to\_i, $3.to\_i)  
irb(main):004:1\> end  
"2009/10/11"  
"2009/10/44"  
ArgumentError: invalid date  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/date.rb:809:in `civil'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `block in irb\_binding'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `each'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/bin/irb19:12:in `\<main\>'  
irb(main):005:0\>

Cheers

robert

> **···**
>
> 2009/9/1 Brian Candler \<b.candler@pobox.com\>:
> 
> > Max Williams wrote:
> > 
> > > date = (string =~ /^\d{4}\/\d{2}\/\d{2}$/) &&
> > 
> > Beware: use
> > 
> > date = (string =~ /\A\d{4}\/\d{2}\/\d{2}\z/) ...
> > 
> > or else malicious inputs may be able to bypass your validation. (They  
> > might not be able to bypass Date.strptime in this particular instance,  
> > but still you ought to validate correctly)
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Ne\_Scripter](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@Ne\_Scripter](https://rubytalk.org/u/Ne_Scripter)\
**Post date:** [7 September 2009 11:43 UTC](https://rubytalk.org/t/date-time-validation/55172/7 "2009-09-07T11:43:19Z")

</div>

Thanks all for the responses. I am still hitting a bit of a block with  
this, initially I want to check if a date matches this criteria, then if  
not do some of the processes that have been suggested.

I need to say  
if date = yyyy/mm/dd  
puts date  
else  
validate date  
end

How would I do this? Sorry for so many questions, I am struggling to get  
my head around the date libraries

Many thanks

Robert Klemme wrote:

> **···**
>
> > 2009/9/1 Brian Candler \<b.candler@pobox.com\>:
> > 
> > > Max Williams wrote:
> > > 
> > > > date = (string =~ /^\d{4}\/\d{2}\/\d{2}$/) &&
> > > 
> > > Beware: use
> > > 
> > > date = (string =~ /\A\d{4}\/\d{2}\/\d{2}\z/) ...
> > > 
> > > or else malicious inputs may be able to bypass your validation. (They  
> > > might not be able to bypass Date.strptime in this particular instance,  
> > > but still you ought to validate correctly)
> > 
> > Then you can do:
> > 
> > 16:48:53 ~$ irb19 -r date  
> > Ruby version 1.9.1  
> > irb(main):001:0\> %w{2009/10/11 2009/10/44}.each do |s|  
> > irb(main):002:1\* p s  
> > irb(main):003:1\> date = %r{\A(\d{4})/(\d{2})/(\d{2})\z} =~ s &&  
> > Date.new($1.to\_i, $2.to\_i, $3.to\_i)  
> > irb(main):004:1\> end  
> > "2009/10/11"  
> > "2009/10/44"  
> > ArgumentError: invalid date  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/date.rb:809:in `civil'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `block in irb\_binding'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `each'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/bin/irb19:12:in `\<main\>'  
> > irb(main):005:0\>
> > 
> > Cheers
> > 
> > robert
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ne\_Scripter](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@Ne\_Scripter](https://rubytalk.org/u/Ne_Scripter)\
**Post date:** [7 September 2009 14:28 UTC](https://rubytalk.org/t/date-time-validation/55172/8 "2009-09-07T14:28:59Z")

</div>

Just an update of where I am. This is what I have

if date =~ (/\d{4}\/\d{2}\/\d{2}/)  
&nbsp;&nbsp;puts date  
else  
&nbsp;&nbsp;date = (Date.strptime(date, "%Y/%m/%d"))  
end

This works to an extent but does not deal with a string which contains a  
valid date but still invalid data like so

2009/09/07 12:12:12

This would be passed as valid when it is not only a string containing  
yyyy/mm/dd.

Any ideas?

Thanks a lot

Ne Scripter wrote:

> **···**
>
> > Thanks all for the responses. I am still hitting a bit of a block with  
> > this, initially I want to check if a date matches this criteria, then if  
> > not do some of the processes that have been suggested.
> > 
> > I need to say  
> > if date = yyyy/mm/dd  
> > puts date  
> > else  
> > validate date  
> > end
> > 
> > How would I do this? Sorry for so many questions, I am struggling to get  
> > my head around the date libraries
> > 
> > Many thanks
> > 
> > Robert Klemme wrote:
> > 
> > > 2009/9/1 Brian Candler \<b.candler@pobox.com\>:
> > > 
> > > > Max Williams wrote:
> > > > 
> > > > > date = (string =~ /^\d{4}\/\d{2}\/\d{2}$/) &&
> > > > 
> > > > Beware: use
> > > > 
> > > > date = (string =~ /\A\d{4}\/\d{2}\/\d{2}\z/) ...
> > > > 
> > > > or else malicious inputs may be able to bypass your validation. (They  
> > > > might not be able to bypass Date.strptime in this particular instance,  
> > > > but still you ought to validate correctly)
> > > 
> > > Then you can do:
> > > 
> > > 16:48:53 ~$ irb19 -r date  
> > > Ruby version 1.9.1  
> > > irb(main):001:0\> %w{2009/10/11 2009/10/44}.each do |s|  
> > > irb(main):002:1\* p s  
> > > irb(main):003:1\> date = %r{\A(\d{4})/(\d{2})/(\d{2})\z} =~ s &&  
> > > Date.new($1.to\_i, $2.to\_i, $3.to\_i)  
> > > irb(main):004:1\> end  
> > > "2009/10/11"  
> > > "2009/10/44"  
> > > ArgumentError: invalid date  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/lib/ruby19/1.9.1/date.rb:809:in `civil'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `block in irb\_binding'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `each'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /opt/bin/irb19:12:in `\<main\>'  
> > > irb(main):005:0\>
> > > 
> > > Cheers
> > > 
> > > robert
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [7 September 2009 16:32 UTC](https://rubytalk.org/t/date-time-validation/55172/9 "2009-09-07T16:32:45Z")

</div>

Ne Scripter wrote:

> Just an update of where I am. This is what I have
> 
> if date =~ (/\d{4}\/\d{2}\/\d{2}/)  
> &nbsp;&nbsp;puts date  
> else  
> &nbsp;&nbsp;date = (Date.strptime(date, "%Y/%m/%d"))  
> end
> 
> This works to an extent but does not deal with a string which contains a  
> valid date but still invalid data like so
> 
> 2009/09/07 12:12:12
> 
> This would be passed as valid when it is not only a string containing  
> yyyy/mm/dd.
> 
> Any ideas?
> 
> Thanks a lot

Look at the regex's in all the following examples. How do they differ  
from your regex?

> > Robert Klemme wrote:
> > 
> > > > Max Williams wrote:
> > > > 
> > > > > /^\d{4}\/\d{2}\/\d{2}$/)
> > > > 
> > > > Beware: use
> > > > 
> > > > /\A\d{4}\/\d{2}\/\d{2}\z/
> > > 
> > > %r{\A(\d{4})/(\d{2})/(\d{2})\z}

..  
.  
.  
.  
.  
.  
.  
..

> **···**
>
> > > > 2009/9/1 Brian Candler \<b.candler@pobox.com\>:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![G\_F](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@G\_F](https://rubytalk.org/u/G_F)\
**Post date:** [7 September 2009 16:56 UTC](https://rubytalk.org/t/date-time-validation/55172/10 "2009-09-07T16:56:06Z")

</div>

Ne Scripter wrote:

> Just an update of where I am. This is what I have
> 
> if date =~ (/\d{4}\/\d{2}\/\d{2}/)  
> &nbsp;&nbsp;puts date  
> else  
> &nbsp;&nbsp;date = (Date.strptime(date, "%Y/%m/%d"))  
> end
> 
> This works to an extent but does not deal with a string which contains a  
> valid date but still invalid data like so
> 
> 2009/09/07 12:12:12
> 
> This would be passed as valid when it is not only a string containing  
> yyyy/mm/dd.

Ok, to summarize to see if I've got it right:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blah 2009/09/07 blah # \<-- is what we want to accept  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blah 2009/09/07 12:00:00 blah # \<-- is what we want to reject

%r#\d{4}/\d{2}/\d{2}# matches a date but gives a false positive for  
dates followed by time.

You could filter out lines that have date followed by time using  
something like:

&nbsp;&nbsp;&nbsp;&nbsp;next if %r#\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2}:\d{2}#

then capture any that make it past that test with a regular date regex.

Or, you can combine the test and capture in one regex pattern using  
look-ahead...

&nbsp;&nbsp;&nbsp;&nbsp;%r#(\d{4}/\d{2}/\d{2})\s+(?!\d{2}:\d{2}:\d{2})# =~ 'blah 2009/01/01  
blah' # =\> 5  
&nbsp;&nbsp;&nbsp;&nbsp;%r#(\d{4}/\d{2}/\d{2})\s+(?!\d{2}:\d{2}:\d{2})# =~ 'blah 2009/01/01  
12:00:00 blah' # =\> nil

The first line successfully matches "text date text".  
The second line rejects "text date time text".

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
