Really quick question - How do I convert a string to a date

Discussion in 'Ruby' started by Glenn Smith, Apr 13, 2005.

  1. Glenn Smith

    Glenn Smith Guest

    I need a 'Date' object which is converted from a string value
    containing something like '13/04/2005' (english format date).

    Help!

    --

    All the best
    Glenn
    Aylesbury, UK
     
    Glenn Smith, Apr 13, 2005
    #1
    1. Advertising

  2. Guillaume Cottenceau, Apr 13, 2005
    #2
    1. Advertising

  3. On Apr 13, 2005, at 8:36 AM, Glenn Smith wrote:

    > I need a 'Date' object which is converted from a string value
    > containing something like '13/04/2005' (english format date).
    >
    > Help!


    This isn't a particularly brilliant answer, so hopefully someone will
    do better:

    ruby -r date -e 'puts Date.parse("13/04/2005".split("/").values_at(1,
    0, 2).join("/"))'

    Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Apr 13, 2005
    #3
  4. Hi,

    Glenn Smith <> writes:

    > I need a 'Date' object which is converted from a string value
    > containing something like '13/04/2005' (english format date).


    % ruby -rdate -e 'puts Date.strptime("13/04/2005", "%d/%m/%Y")'
    2005-04-13

    --
    eban
     
    WATANABE Hirofumi, Apr 13, 2005
    #4
  5. Farrel Lifson, Apr 13, 2005
    #5
  6. On Apr 13, 2005, at 8:49 AM, WATANABE Hirofumi wrote:

    > Hi,
    >
    > Glenn Smith <> writes:
    >
    >> I need a 'Date' object which is converted from a string value
    >> containing something like '13/04/2005' (english format date).

    >
    > % ruby -rdate -e 'puts Date.strptime("13/04/2005", "%d/%m/%Y")'
    > 2005-04-13


    I knew there had to be a clever solution. Nice!

    James Edward Gray II
     
    James Edward Gray II, Apr 13, 2005
    #6
  7. Glenn Smith

    Neil Stevens Guest

    On Wed, 13 Apr 2005 23:36:10 +0900, Glenn Smith wrote:

    > I need a 'Date' object which is converted from a string value
    > containing something like '13/04/2005' (english format date).
    >
    > Help!


    If you're certain it's that date format:

    require 'date'
    parts = '13/04/2005'.split('/').reverse
    parts = parts.collect do |i|
    i = i.to_i
    end
    date = Date.new(*parts)

    --
    Neil Stevens -

    'A republic, if you can keep it.' -- Benjamin Franklin
     
    Neil Stevens, Apr 13, 2005
    #7
  8. Glenn Smith

    Glenn Smith Guest

    Thanks guys - I'm getting jumpy about my demo tomorrow!
    G

    On 4/13/05, James Edward Gray II <> wrote:
    > On Apr 13, 2005, at 8:49 AM, WATANABE Hirofumi wrote:
    >
    > > Hi,
    > >
    > > Glenn Smith <> writes:
    > >
    > >> I need a 'Date' object which is converted from a string value
    > >> containing something like '13/04/2005' (english format date).

    > >
    > > % ruby -rdate -e 'puts Date.strptime("13/04/2005", "%d/%m/%Y")'
    > > 2005-04-13

    >
    > I knew there had to be a clever solution. Nice!
    >
    > James Edward Gray II
    >
    >



    --

    All the best
    Glenn
    Aylesbury, UK
     
    Glenn Smith, Apr 13, 2005
    #8
  9. Glenn Smith

    Neil Stevens Guest

    On Wed, 13 Apr 2005 23:50:12 +0900, Farrel Lifson wrote:

    > ParseDate? It's in Ruby's Std Lib -
    > http://www.rubycentral.com/book/lib_standard.html#ParseDate.parsedate


    Nope, that assumes month/day/year when it sees that style. It can only
    support one or the other thanks to cases like '1/2/2005'.

    --
    Neil Stevens -

    'A republic, if you can keep it.' -- Benjamin Franklin
     
    Neil Stevens, Apr 13, 2005
    #9
  10. Glenn Smith

    Glenn Smith Guest

    It was exactly this problem I was having Neil (ie. using ParseDate on
    '13/04'2005' gave me an error - I think it didn't like there being 13
    months!)

    On 4/13/05, Neil Stevens <> wrote:
    > On Wed, 13 Apr 2005 23:50:12 +0900, Farrel Lifson wrote:
    >
    > > ParseDate? It's in Ruby's Std Lib -
    > > http://www.rubycentral.com/book/lib_standard.html#ParseDate.parsedate

    >
    > Nope, that assumes month/day/year when it sees that style. It can only
    > support one or the other thanks to cases like '1/2/2005'.
    >
    > --
    > Neil Stevens -
    >
    > 'A republic, if you can keep it.' -- Benjamin Franklin
    >
    >



    --

    All the best
    Glenn
    Aylesbury, UK
     
    Glenn Smith, Apr 14, 2005
    #10
    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. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,300
    Michael Borgwardt
    May 30, 2004
  2. wgan
    Replies:
    7
    Views:
    606
    Roedy Green
    Jul 8, 2004
  3. Jeannie
    Replies:
    15
    Views:
    901
    Jeannie
    Aug 30, 2005
  4. Replies:
    8
    Views:
    792
    Boris
    May 1, 2007
  5. perl quick date convert

    , Feb 1, 2005, in forum: Perl Misc
    Replies:
    7
    Views:
    169
    Tad McClellan
    Feb 3, 2005
Loading...

Share This Page