Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails get difference between two dates [duplicate]

How to get time difference in days,hours,mins

I am trying to do

datetime_A - datetime_B

datetime_A = Sat, 04 Jan 2014 07:00:13 +0000

datetime_B = Fri, 03 Jan 2014 01:09:46 +0000

it returns me something like(35809/28800),bdw what does it means?

I need like 1day,5h,23min

How can do it?

like image 581
user Avatar asked Dec 26 '14 10:12

user


2 Answers

I like to use the gem time_diff

https://github.com/abhidsm/time_diff

Here is a example how it works

   Time.diff(Time.parse('2011-03-06'), Time.parse('2011-03-07'))
   # => {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 day and 00:00:00'}
like image 88
MZaragoza Avatar answered Sep 19 '22 17:09

MZaragoza


An alternative could be to use the ActionView helper distance_of_time_in_words (see the documentation). If normally does not give the exact minor units, but something like "around 5 days".

like image 29
mliebelt Avatar answered Sep 18 '22 17:09

mliebelt