我如何将此时间字符串转换为ruby的纪元时间?
我如何将2012-12-13T14:43:35.371Z转换为纪元时间?
我正在考虑的一种方法是剥离T和Z,然后尝试使用DateTime库来找到转换为纪元的方法。 在我沿着这条路走下去之前,我很好奇是否有一块gem可以用来快速完成。 在相关的说明中,我将在转换为纪元后进行时间计算。
例如:
require 'date' # as an integer: DateTime.parse('2012-12-13T14:43:35.371Z').to_time.to_i # or as a string: DateTime.parse('2012-12-13T14:43:35.371Z').strftime('%s')
require "time" Time.iso8601("2012-12-13T14:43:35.371Z").to_i
require "date" DateTime.parse("2012-12-13T14:43:35.371Z").strftime("%s")
如果您需要更多地控制解析,请使用strptime
,它允许您定义解释格式的字符串。