Getting current date

Discussion in 'Ruby' started by laredotornado, Sep 12, 2008.

  1. Hi,

    How do I get the current date in the form "YYYY-MM-DD".

    Thanks, - Davegro
     
    laredotornado, Sep 12, 2008
    #1
    1. Advertising

  2. My clunky way:

    n = Time.now
    d = Time.gm(n.year, n.month - 1, n.day)

    p d

    => Tue Aug 12 00:00:00 UTC 2008
    --
    Posted via http://www.ruby-forum.com/.
     
    Lloyd Linklater, Sep 12, 2008
    #2
    1. Advertising

  3. ok. Improvement time:

    n = Time.now
    if n.month == 12
    d = Time.gm(n.year - 1, 12, n.day)
    else
    d = Time.gm(n.year, 1 - 1, n.day)
    end


    There has GOT to be a better way but I did not want you to have nothing.
    :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Lloyd Linklater, Sep 12, 2008
    #3
  4. dang typos

    n = Time.now
    if n.month == 1
    d = Time.gm(n.year - 1, 12, n.day)
    else
    d = Time.gm(n.year, 1 - 1, n.day)
    end


    --
    Posted via http://www.ruby-forum.com/.
     
    Lloyd Linklater, Sep 12, 2008
    #4
  5. I put the testing code in and I feel REALLY stupid. If it is still
    wrong, I am going to hide under a rock until everyone forgets that I did
    this.

    n = Time.now
    if n.month == 1
    d = Time.gm(n.year - 1, 12, n.day)
    else
    d = Time.gm(n.year, n.month - 1, n.day)
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    Lloyd Linklater, Sep 12, 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. James P.
    Replies:
    7
    Views:
    12,518
    phamtasmic
    Jul 13, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,269
    Michael Borgwardt
    May 30, 2004
  3. moonhk
    Replies:
    5
    Views:
    544
    moonhk
    Oct 11, 2006
  4. Scamjunk

    getting current date in C in the VC environ

    Scamjunk, Jan 13, 2006, in forum: C Programming
    Replies:
    7
    Views:
    298
    Richard Heathfield
    Jan 19, 2006
  5. Michael Haas

    compare date field to current date

    Michael Haas, Feb 15, 2004, in forum: ASP General
    Replies:
    4
    Views:
    286
    Mike Haas
    Feb 15, 2004
Loading...

Share This Page