Ruby Time object issues

I faced this problem when I try to deduct database stored time objects. My self and I think most of developers use time object to store time. But time objedct create and store with an aditional information like year, month and time, etc.

So when we try to decduct two time objects it just not give us only the time difference as what we belive. It calculate the difference of both time object not only considering the time but also with considering year, month and day difference.

So be aware about that when u are calculating the time difference and I mention some methods to overcome that issue in below.

(time_object_2) – (time_object_1)%86400

this will give only the time difference. Other than that u can cast all the time object to a current time object year, month and day by using

today=Time.now #today time object

time_object # which is the time object we neet to cast for certain year month and day

cast_time_object=Time.mktime(today.year,today.month,today.day, time_object.hour, time_object.min, time_object.sec,0)

code. Now we can deduct time objects and we can calculate correct time different.


Leave a Reply