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 th开发者_高级运维e past. How would I find the difference between the two, and transform it into seconds?
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
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.
精彩评论