开发者

RoR Time into Seconds Question

开发者 https://www.devze.com 2023-02-18 23:04 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号