Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > setting date format to validate date in ruby

Reply
Thread Tools

setting date format to validate date in ruby

 
 
Jay Pangmi
Guest
Posts: n/a
 
      08-24-2008
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Thomas B.
Guest
Posts: n/a
 
      08-24-2008
Jay Pangmi wrote:
> Hi, I'm just learning ruby. I was searching for date validation in ruby


You can have a look at this post:
http://al2o3-cr.blogspot.com/2008/08...ding-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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jay Pangmi
Guest
Posts: n/a
 
      08-24-2008
Thomas B. wrote:
> You can have a look at this post:
> http://al2o3-cr.blogspot.com/2008/08...ding-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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Thomas B.
Guest
Posts: n/a
 
      08-24-2008
Jay Pangmi wrote:
> Thomas B. wrote:
>> You can have a look at this post:
>> http://al2o3-cr.blogspot.com/2008/08...ding-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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Suraj Kurapati
Guest
Posts: n/a
 
      08-25-2008
Thomas B. wrote:
> Jay Pangmi wrote:
>> Thomas B. wrote:
>>> You can have a look at this post:
>>> http://al2o3-cr.blogspot.com/2008/08...ding-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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate that a String is a particular date format laredotornado Java 2 08-28-2008 02:45 AM
use CompareValidator to validate date format Neo ASP .Net 2 11-28-2007 11:50 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM
Validate date format Ana Rita ASP .Net Web Controls 0 09-19-2003 01:07 PM
Validate date format Ana Rita ASP .Net Web Controls 1 09-19-2003 10:49 AM



Advertisments