将具有UTC时间的变量转换为Seconds整数(Ruby)(Converting a variable with a UTC time to a Seconds integer (Ruby))

编程入门 行业动态 更新时间:2024-10-27 00:30:05
将具有UTC时间的变量转换为Seconds整数(Ruby)(Converting a variable with a UTC time to a Seconds integer (Ruby))

我有一个与它相关的UTC时间的变量,例如:

offer_ends_at => Sun, 25 Nov 2012 07:59:59 UTC +00:00,

我的目标是创建一个新变量,并将Time转换为秒整数。 我尝试过一些类似的方法

seconds = offer_ends_at.to_time.to_i

我最终获得了一段时间。 我在一个实际的整数之后,它包含到日期发生之前剩余的秒数。

I have a variable that has a UTC time tied with it, example:

offer_ends_at => Sun, 25 Nov 2012 07:59:59 UTC +00:00,

My goal is to create a new variable, and convert the Time to a seconds integer. I tried with some methods like

seconds = offer_ends_at.to_time.to_i

I end up getting a epoc time. I'm after an actual integer that contains the amount of seconds left until the date happens.

最满意答案

我想要“一个包含到日期发生之前剩余的秒数的实际整数。”,然后你可以将你得到的时间戳减去当前的时间戳,即:

seconds = offer_ends_at.to_time.to_i - Time.now.to_i

I you want " an actual integer that contains the amount of seconds left until the date happens.", then you can substract the timestamp you're getting, to the current timestamp, ie :

seconds = offer_ends_at.to_time.to_i - Time.now.to_i

更多推荐

本文发布于:2023-08-06 22:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457466.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   转换为   变量   时间   Seconds

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!