# To calculate differnece between two time variables

**URL:** <https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663>\
**Category:** Rails\
**Created:** [June 17, 2008, 9:59am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663 "2008-06-17T09:59:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 17, 2008, 9:59am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663/1 "2008-06-17T09:59:36Z")

</div>

Hi  
In the table I have a field updated\_on (type :datetime)…I have to  
calculate the differnce in hours between that and current time…How can  
I do that?

```
 I tried like for example
  time1=TimeTable.find(1).updated_on
  time2=Time.new
  puts (time1-time2) but it is not working Prints a very large

```

value…Could you please tell me any easier method?

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 17, 2008, 10:55am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663/2 "2008-06-17T10:55:19Z")

</div>

On 17 Jun 2008, at 08:59, Sijo Kg wrote:

> ```
> puts (time1-time2) but it is not working Prints a very large
> 
> ```
> 
> value…Could you please tell me any easier method?

It is working, but that gives you the difference in seconds.  
converting it to hours (divide by 3600) should be an easy exercise.

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 17, 2008, 11:04am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663/3 "2008-06-17T11:04:46Z")

</div>

Hi  
thanks for your reply…One more question related to this…In my  
ServiceDeskTicket model i have two fields  
1.updted\_on  
2.service\_desk\_status\_id  
I have to find out all records with time difference between  
updated\_on and current time \> 48 hours and service\_desk\_status\_id=6.  
Could you please tell me how I can write this?

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 17, 2008, 11:11am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663/4 "2008-06-17T11:11:57Z")

</div>

On 17 Jun 2008, at 10:04, Sijo Kg wrote:

> Hi  
> thanks for your reply…One more question related to this…In my  
> ServiceDeskTicket model i have two fields  
> 1.updted\_on  
> 2.service\_desk\_status\_id  
> I have to find out all records with time difference between  
> updated\_on and current time \> 48 hours and service\_desk\_status\_id=6.  
> Could you please tell me how I can write this?

that’s even easier, you just need to find records where updated\_on is  
more than 48 hours ago. Rails even provides some helpers here:  
48.hours.ago will evaluate to that time.

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 17, 2008, 11:14am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663/5 "2008-06-17T11:14:39Z")

</div>

Hi  
thanks for this information

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 17, 2008, 11:33am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663/6 "2008-06-17T11:33:48Z")

</div>

On 17 Jun 2008, at 10:30, Sijo Kg wrote:

> before 48 hours) but I get the opposite that is 20 Total 30 records in  
> table…Am I missing something?Is the condition wrong?  
> You put \> instead of \<

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 17, 2008, 11:30am UTC](https://www.ruby-forum.com/t/to-calculate-differnece-between-two-time-variables/140663/7 "2008-06-17T11:30:56Z")

</div>

Hi  
I tried like  
time\_array=ServiceDeskTicket.find :all, :conditions=\>["updated\_on \> ?  
",48.hours.ago]  
puts time\_array

```
   But actually I have to get 10 records .ie (10 records are updated 

```

before 48 hours) but I get the opposite that is 20 Total 30 records in  
table…Am I missing something?Is the condition wrong?

Sijo
