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.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2008-11-27 06: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
59193d8082ee0fe75675d3bb376cf2ba?d=identicon&s=25 Pierre Pierre (pierre_p)
on 2008-11-27 06: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.
Ee469623eb1b8e6e35d192822b9c4aa2?d=identicon&s=25 Florian Aßmann (Guest)
on 2008-11-27 08:03
(Received via mailing list)
(Time.now - incidents.first.created_on) / 1.hour

Am 27.11.2008 um 06:48 schrieb Pierre Pat:
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2008-11-27 09:19
Hi
   Thanks for the reply
Sijo
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-11-27 12:09
(Received via mailing list)
On Thu, Nov 27, 2008 at 7:57 AM, Florian Aßmann
<florian.assmann@email.de> 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 Dober ;)
F50f5d582d76f98686da34917531fe56?d=identicon&s=25 Peter Szinek (Guest)
on 2008-11-27 13:11
(Received via mailing list)
On 2008.11.27., at 12:03, Robert Dober wrote:

> On Thu, Nov 27, 2008 at 7:57 AM, Florian Aßmann
> <florian.assmann@email.de> 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
Ee469623eb1b8e6e35d192822b9c4aa2?d=identicon&s=25 Florian Aßmann (Guest)
on 2009-02-16 00: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 Dober:
This topic is locked and can not be replied to.