将具有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_iI 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_iI 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更多推荐
发布评论