From: Robert Klemme Date: 2009-09-01T22:33:34+09:00 Subject: Re: Date & time validation 2009/9/1 Ne Scripter : > 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! 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 from /opt/lib/ruby19/1.9.1/time.rb:202:in `local' from /opt/lib/ruby19/1.9.1/time.rb:202:in `make_time' from /opt/lib/ruby19/1.9.1/time.rb:261:in `parse' from (irb):2 from /opt/bin/irb19:12:in `
' irb(main):003:0> Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/