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

Thanks.

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:
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.

Thomas B. wrote:

You can have a look at this post:
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.

# 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

Jay P. wrote:

Thomas B. wrote:

You can have a look at this post:
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.

Thomas B. wrote:

Jay P. wrote:

Thomas B. wrote:

You can have a look at this post:
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.