# Setting date format to validate date in ruby

**URL:** <https://www.ruby-forum.com/t/setting-date-format-to-validate-date-in-ruby/146692>\
**Category:** Ruby\
**Created:** [August 24, 2008, 6:39am UTC](https://www.ruby-forum.com/t/setting-date-format-to-validate-date-in-ruby/146692 "2008-08-24T06:39:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [August 24, 2008, 6:39am UTC](https://www.ruby-forum.com/t/setting-date-format-to-validate-date-in-ruby/146692/1 "2008-08-24T06:39:13Z")

</div>

# Hi, I’m just learning ruby. I was searching for date validation in ruby and found this:

# for date in dateArray if %r{(\d\d)/(\d\d)/(\d\d\d\d)} =~ date mday, month, year = $1.to\_i, $2.to\_i, $3.to\_i puts date if Date.valid\_civil?(year, month, mday) else puts “Invalid Format” end end

# This seemed to do the job but found out not. The problem is it only validates the 01/12/2008 but 1/2/2008 fails. Also the date with different separater fails as well. For 1/2/2008 such dates I tried the following:

# for date in dateArray if %r{(\d{1,2})/(\d{1,2})/(\d{4})} =~ date mday, month, year = $1.to\_i, $2.to\_i, $3.to\_i puts date if Date.valid\_civil?(year,month,mday) else puts “Invalid Format” end end

# For some strange reason, it only iterates thru 5 dates though I have seven dates in dateArray. But for the five it does it right. So, I’m wondering why? also what can I do to give different options for separators like “/,:,-,” or space. Though the original code is not my invention I am wondering what does this line do in particular:

# … mday, month, year = $1.to\_i, $2.to\_i, $3.to\_i …

Thanks.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [August 24, 2008, 10:11am UTC](https://www.ruby-forum.com/t/setting-date-format-to-validate-date-in-ruby/146692/2 "2008-08-24T10:11:01Z")

</div>

Jay P. wrote:

> Hi, I’m just learning ruby. I was searching for date validation in ruby

You can have a look at this post:  
[Ruby (Al2O3::Cr)](http://al2o3-cr.blogspot.com/2008/08/downloading-file.html) . There’s  
date validation in it, in the very general form, working with any  
separator and any number of digits. It uses scanning for digit groups.

TPR.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [August 24, 2008, 10:54am UTC](https://www.ruby-forum.com/t/setting-date-format-to-validate-date-in-ruby/146692/3 "2008-08-24T10:54:33Z")

</div>

Thomas B. wrote:

> You can have a look at this post:  
> [Ruby (Al2O3::Cr)](http://al2o3-cr.blogspot.com/2008/08/downloading-file.html) . There’s  
> date validation in it, in the very general form, working with any  
> separator and any number of digits. It uses scanning for digit groups.
> 
> TPR.

# wow… man that was cool, specially for the beginner like me. Thanks a tonn. Anyway, I figured one solution out for me here’s the code:

# for mydate in dateArray if %r{(\d{1,2})/(\d{1,2})/(\d{4})} =~ mydate or %r{(\d{1,2}):(\d{1,2}):(\d{4})} =~ mydate or %r{(\d{1,2}) (\d{1,2}) (\d{4})} =~ mydate mday, month, year = $1.to\_i, $2.to\_i, $3.to\_i if Date.valid\_civil?(year,month,mday) #virtualArray \<\< mydate puts "Valid Date: " \<\< mydate else puts "Invalid Date: " \<\< mydate end end end

One really cool thing about Date.valid\_civil? is that it return false  
even if the date doesn’t exist in the calender no matter how right the  
format is.  
Now what is bothering me is the logic to sort out the dates in the  
array. I tried the simplest way “Array.sort” but it only sorted the  
array according to the first value it sees not the whole date. How can I  
sort it out. Thanks

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [August 24, 2008, 11:12am UTC](https://www.ruby-forum.com/t/setting-date-format-to-validate-date-in-ruby/146692/4 "2008-08-24T11:12:54Z")

</div>

Jay P. wrote:

> Thomas B. wrote:
> 
> > You can have a look at this post:  
> > [Ruby (Al2O3::Cr)](http://al2o3-cr.blogspot.com/2008/08/downloading-file.html) . There’s  
> > date validation in it, in the very general form, working with any  
> > separator and any number of digits. It uses scanning for digit groups.
> 
> Now what is bothering me is the logic to sort out the dates in the  
> array. I tried the simplest way “Array.sort” but it only sorted the  
> array according to the first value it sees not the whole date. How can I  
> sort it out. Thanks

Use the same approach as is used in the post I gave you link to:

- create Date objects for each date (once you parse it and see that it  
is correct), and put the dates in an array,
- sort the array of dates, they will get sorted OK,
- convert dates back to strings if you need to, using Date#strftime.

TPR.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [August 25, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/setting-date-format-to-validate-date-in-ruby/146692/5 "2008-08-25T18:02:12Z")

</div>

Thomas B. wrote:

> Jay P. wrote:
> 
> > Thomas B. wrote:
> > 
> > > You can have a look at this post:  
> > > [Ruby (Al2O3::Cr)](http://al2o3-cr.blogspot.com/2008/08/downloading-file.html) . There’s  
> > > date validation in it, in the very general form, working with any  
> > > separator and any number of digits. It uses scanning for digit groups.
> > 
> > Now what is bothering me is the logic to sort out the dates in the  
> > array. I tried the simplest way “Array.sort” but it only sorted the  
> > array according to the first value it sees not the whole date. How can I  
> > sort it out. Thanks
> 
> Use the same approach as is used in the post I gave you link to:
> 
> - create Date objects for each date (once you parse it and see that it  
> is correct), and put the dates in an array,
> - sort the array of dates, they will get sorted OK,
> - convert dates back to strings if you need to, using Date#strftime.

This can all be done _temporarily_ if you use Array#sort\_by:

string\_dates = […]  
result = string\_dates.sort\_by {|s| Date.parse s }

Assuming that Date.parse is how you convert your string dates into Date  
objects.
