Justin Meltzer
Justin Meltzer

Reputation: 13558

RoR Time into Seconds Question

Let's say I want to find out the difference in seconds between two times. One of the times is the created_at attribute of the element, and the other time is a random fixed time in the past. How would I find the difference between the two, and transform it into seconds?

Upvotes: 0

Views: 623

Answers (2)

npad
npad

Reputation: 5046

It should be as simple as:

time = Time.now
offset = time - record.created_at

offset will now be a Float which is the difference in seconds between the two compared Time objects.

Upvotes: 2

Ceilingfish
Ceilingfish

Reputation: 5455

Lucky for you the - operator in ruby returns you an float which is the difference in seconds.

difference_in_seconds = x.created_at - random_time_in_past

Upvotes: 4

Related Questions