date time subtraction

Discussion in 'Ruby' started by aidy, Jul 1, 2009.

  1. aidy

    aidy Guest

    Hi,

    I am trying to do a subtraction of the date and time in the format
    below with the current date and time

    "2009-07-01 18:24:20"

    Will I need to tokenise this string and do individual number
    comparisons?

    Thanks

    Aidy
     
    aidy, Jul 1, 2009
    #1
    1. Advertising

  2. aidy wrote:
    > Hi,
    >
    > I am trying to do a subtraction of the date and time in the format
    > below with the current date and time
    >
    > "2009-07-01 18:24:20"
    >
    > Will I need to tokenise this string and do individual number
    > comparisons?


    irb(main):007:0> require 'time'
    => true
    irb(main):009:0> t = Time.parse "2009-07-01 18:24:20"
    => Wed Jul 01 18:24:20 -0700 2009
    irb(main):010:0> t.to_i
    => 1246497860
    irb(main):011:0> t.day
    => 1
    irb(main):012:0> t.hour
    => 18

    See the Time class for details. You can also directly compare time
    objects (without using to_i to get seconds since the epoch):

    irb(main):013:0> Time.now < Time.now
    => true

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jul 1, 2009
    #2
    1. Advertising

  3. Joel VanderWerf wrote:
    > See the Time class for details. You can also directly compare time
    > objects (without using to_i to get seconds since the epoch):


    And you can also subtract Time objects to get seconds:

    irb(main):016:0> t - Time.now
    => 27032.573318

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jul 1, 2009
    #3
  4. aidy

    aidy Guest

    On 1 July, 18:50, Joel VanderWerf <> wrote:

    >
    > irb(main):007:0> require 'time'
    > => true
    > irb(main):009:0> t = Time.parse "2009-07-01 18:24:20"
    > => Wed Jul 01 18:24:20 -0700 2009


    Thanks Joel.

    Aidy
     
    aidy, Jul 1, 2009
    #4
    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. Chris Berg
    Replies:
    0
    Views:
    829
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,332
    Michael Borgwardt
    May 30, 2004
  3. rsutradhar_python

    Date Subtraction

    rsutradhar_python, Jun 17, 2006, in forum: Python
    Replies:
    3
    Views:
    441
    Marc 'BlackJack' Rintsch
    Jun 18, 2006
  4. Keith Cochrane
    Replies:
    2
    Views:
    635
    Keith Cochrane
    Aug 6, 2006
  5. Summu82
    Replies:
    5
    Views:
    890
    Richard Heathfield
    Jun 7, 2006
Loading...

Share This Page