Forum: Ruby To get time differnece in hours between two time values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sijo k. (Guest)
on 2008-11-27 07:21
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
Pierre P. (Guest)
on 2008-11-27 07:53
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.
Florian Aßmann (Guest)
on 2008-11-27 09:03
(Received via mailing list)
(Time.now - incidents.first.created_on) / 1.hour

Am 27.11.2008 um 06:48 schrieb Pierre P.:
Sijo k. (Guest)
on 2008-11-27 10:19
Hi
   Thanks for the reply
Sijo
Robert D. (Guest)
on 2008-11-27 13:09
(Received via mailing list)
On Thu, Nov 27, 2008 at 7:57 AM, Florian Aßmann
<removed_email_address@domain.invalid> 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
--
Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert D. ;)
Peter S. (Guest)
on 2008-11-27 14:11
(Received via mailing list)
On 2008.11.27., at 12:03, Robert D. wrote:

> On Thu, Nov 27, 2008 at 7:57 AM, Florian Aßmann
> <removed_email_address@domain.invalid> 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,
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
Florian Aßmann (Guest)
on 2009-02-16 01:42
(Received via mailing list)
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
these nn chars into this e-mail.

Cheers
Florian

Am 27.11.2008 um 12:03 schrieb Robert D.:
This topic is locked and can not be replied to.