Trip Trip - 1 month ago 11
Ruby Question

How do you subtract from a datetime?

I'd like to write a simple function to say in hours:

How much time has passed since this has been created?

My attempts:

-time = DateTime.now.hour - (self.created_at.hour)


Does anyone know how to do this in Ruby on Rails?

Answer

Rails usually uses Time, not DateTime. Why don't you do Time.now - self.created_at? Then you can convert from seconds to hours by dividing by 3600.