新手问题 怎么把 Double 转化为 Time 字符串

bluexuemei · January 08, 2015 · Last by bluexuemei replied at January 08, 2015 · 2297 hits

如:0.5 怎么转化为“12:00:00”???

0.5 表示半天????

楼主没说转换的规则是什么。如果如楼上 @est 说的,那试试这样:

def float_to_time(v)
  raise RangeError, ":(~~~" if v < 0 or v > 1
  Time.at(v * 86400).utc.strftime("%H:%M:%S")
end

p float_to_time 0.5

Time.at(v * 86400).utc.strftime("%H:%M:%S"),这个不错

#2 楼 @skandhas ,再问一下,怎么把“12:00:00”转化为 0.5???

#4 楼 @bluexuemei 先求得秒,然后除以 86400 就可以了。

require 'time'

def time_to_float(s)
  (Time.parse(s) - Time.parse("00:00:00")) / 86400
end
You need to Sign in before reply, if you don't have an account, please Sign up first.