To get time differnece in hours between two time values

Discussion in 'Ruby' started by Sijo Kg, Nov 27, 2008.

  1. Sijo Kg

    Sijo Kg Guest

    Hi
    I have two values like incidents.first.created_on and Time.now
    bothe are of class Time Now I would like to get differnet between these
    two in hours I gtried like

    (Time.now-incidents.first.created_on).hours
    But getting wrong result..Could you please tell how I can do this?

    Thanks in advance
    Sijo
    --
    Posted via http://www.ruby-forum.com/.
     
    Sijo Kg, Nov 27, 2008
    #1
    1. Advertising

  2. Sijo Kg

    Pierre Pat Guest

    Sijo Kg wrote:
    > Hi
    > I have two values like incidents.first.created_on and Time.now
    > bothe are of class Time Now I would like to get differnet between these
    > two in hours I gtried like
    >
    > (Time.now-incidents.first.created_on).hours
    > But getting wrong result..Could you please tell how I can do this?
    >
    > Thanks in advance
    > Sijo


    Time.now-incidents.first.created_on will return you the number of
    seconds in Numeric, that's why you can't call the method 'hours' on it.
    I haven't seen a method which would convert seconds in hours for you, if
    there really isn't one, you'll have to make the convertion to hours
    yourself...

    Cheers.
    --
    Posted via http://www.ruby-forum.com/.
     
    Pierre Pat, Nov 27, 2008
    #2
    1. Advertising

  3. (Time.now - incidents.first.created_on) / 1.hour

    Am 27.11.2008 um 06:48 schrieb Pierre Pat:

    > Sijo Kg wrote:
    >> Hi
    >> I have two values like incidents.first.created_on and Time.now
    >> bothe are of class Time Now I would like to get differnet between
    >> these
    >> two in hours I gtried like
    >>
    >> (Time.now-incidents.first.created_on).hours
    >> But getting wrong result..Could you please tell how I can do
    >> this?
    >>
    >> Thanks in advance
    >> Sijo

    >
    > Time.now-incidents.first.created_on will return you the number of
    > seconds in Numeric, that's why you can't call the method 'hours' on
    > it.
    > I haven't seen a method which would convert seconds in hours for
    > you, if
    > there really isn't one, you'll have to make the convertion to hours
    > yourself...
    >
    > Cheers.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
     
    Florian Aßmann, Nov 27, 2008
    #3
  4. Sijo Kg

    Sijo Kg Guest

    Sijo Kg, Nov 27, 2008
    #4
  5. Sijo Kg

    Robert Dober Guest

    On Thu, Nov 27, 2008 at 7:57 AM, Florian A=DFmann
    <> wrote:
    > (Time.now - incidents.first.created_on) / 1.hour

    I do not think that 1 responds to that message.
    but using 3600 instead should do the trick.
    HTH
    Robert
    --=20
    Ne baisse jamais la t=EAte, tu ne verrais plus les =E9toiles.

    Robert Dober ;)
     
    Robert Dober, Nov 27, 2008
    #5
  6. Sijo Kg

    Peter Szinek Guest

    On 2008.11.27., at 12:03, Robert Dober wrote:

    > On Thu, Nov 27, 2008 at 7:57 AM, Florian A=DFmann
    > <> wrote:
    >> (Time.now - incidents.first.created_on) / 1.hour

    > I do not think that 1 responds to that message.


    Neither do I...
    However, if you are using Rails and ok with requiring active_support, =20=

    that enables 1.hour for you.

    > but using 3600 instead should do the trick.


    In this case I'd vote for this option.

    Cheers,
    Peter
    ___
    http://www.rubyrailways.com
    http://scrubyt.org=
     
    Peter Szinek, Nov 27, 2008
    #6
  7. Hi Robert,

    the original code:

    (Time.now-incidents.first.created_on).hours

    almost implies that it uses a framework like Rails so I naively blown =20=

    these nn chars into this e-mail.

    Cheers
    Florian

    Am 27.11.2008 um 12:03 schrieb Robert Dober:

    > On Thu, Nov 27, 2008 at 7:57 AM, Florian A=DFmann
    > <> wrote:
    >> (Time.now - incidents.first.created_on) / 1.hour

    > I do not think that 1 responds to that message.
    > but using 3600 instead should do the trick.
    > HTH
    > Robert
    > --=20
    > Ne baisse jamais la t=EAte, tu ne verrais plus les =E9toiles.
    >
    > Robert Dober ;)
    >
     
    Florian Aßmann, Feb 15, 2009
    #7
    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. Gopal Prabhakaran

    Differnece between Events and Delegates

    Gopal Prabhakaran, Jan 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    7,167
    Gaurav Khanna [C# MVP]
    Jan 28, 2004
  2. Rahmi Acar
    Replies:
    0
    Views:
    576
    Rahmi Acar
    Jul 17, 2003
  3. baumann@pan
    Replies:
    1
    Views:
    767
    Richard Bos
    Apr 15, 2005
  4. Tagore

    Any differnece between functions

    Tagore, Jan 23, 2009, in forum: C Programming
    Replies:
    13
    Views:
    532
    jaysome
    Jan 24, 2009
  5. rutherf
    Replies:
    2
    Views:
    459
    rutherf
    Oct 28, 2006
Loading...

Share This Page