Ruby: Convert time to seconds?

后端 未结 8 713
甜味超标
甜味超标 2020-12-28 08:16

How can I convert a time like 10:30 to seconds? Is there some sort of built in Ruby function to handle that?

Basically trying to figure out the number of seconds fro

8条回答
  •  借酒劲吻你
    2020-12-28 08:32

    I like these answers very much, especially Teddy's for its tidyness.

    There's one thing to note. Teddy's answer gives second of day in current region and I haven't been able to convert Date.today.to_time to UTC. I ended up with this workaround:

    Time.now.to_i % 86400
    

    It's based on the fact that Time.now.to_i gives seconds since Unix Epoch which is always 1970-01-01T00:00:00Z, regardless of your current time zone. And the fact that there's 86400 seconds in a day as well. So this solution will always give you seconds since last UTC midnight.

提交回复
热议问题