0

我正在尝试获取一个字符串"00:04:55",并将其解析为某种 Ruby 对象,以便获得总秒数。类似于 C# 中的简单timespan.

4

2 回答 2

2

使用Time.strptime会做你想做的事(我认为):

> require 'time' # not needed inside rails
> time = Time.strptime("06:04:55", "%H:%M:%S")
> time.sec
55
> time.min
4
> time.hour
6
> require 'active_support/core_ext' # not needed in rails
> time.seconds_since_midnight.to_i
21895
于 2013-10-26T02:39:20.967 回答
1

您可以使用一个技巧,它不是很优雅,但应该这样做:

Date.parse(Time.now.strftime("%d")+' '+"00:04:55")
# => #<Date: 2013-10-26 ((2456592j,0s,0n),+0s,2299161j)>
于 2013-10-26T01:45:23.560 回答