to calculate differnece between two time variables

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

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

Fred

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

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

Hi     thanks for this information

Sijo

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

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?

You put > instead of <

Fred