setting date format to validate date in ruby

Discussion in 'Ruby' started by Jay Pangmi, Aug 24, 2008.

  1. Jay Pangmi

    Jay Pangmi Guest

    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/.
    Jay Pangmi, Aug 24, 2008
    #1
    1. Advertising

  2. Jay Pangmi

    Thomas B. Guest

    Thomas B., Aug 24, 2008
    #2
    1. Advertising

  3. Jay Pangmi

    Jay Pangmi Guest

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

    --
    Posted via http://www.ruby-forum.com/.
    Jay Pangmi, Aug 24, 2008
    #3
  4. Jay Pangmi

    Thomas B. Guest

    Jay Pangmi wrote:
    > Thomas B. wrote:
    >> You can have a look at this post:
    >> 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.
    --
    Posted via http://www.ruby-forum.com/.
    Thomas B., Aug 24, 2008
    #4
  5. Thomas B. wrote:
    > Jay Pangmi wrote:
    >> Thomas B. wrote:
    >>> You can have a look at this post:
    >>> 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.
    --
    Posted via http://www.ruby-forum.com/.
    Suraj Kurapati, Aug 25, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,159
    Janaka
    Feb 18, 2004
  2. Neo
    Replies:
    2
    Views:
    7,848
    adamyang
    Nov 28, 2007
  3. laredotornado
    Replies:
    2
    Views:
    1,297
    Roedy Green
    Aug 28, 2008
  4. Ana Rita

    Validate date format

    Ana Rita, Sep 19, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    174
    Ravikanth{MVP]
    Sep 19, 2003
  5. Ana Rita

    Validate date format

    Ana Rita, Sep 19, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    124
    Ana Rita
    Sep 19, 2003
Loading...

Share This Page