将刻度线解析为日期时间

如何将tick(例如1298011537289转换为1298011537289中的DateTime?

我需要转换的值来自JavaScript Date.now()调用,因此它以毫秒为单位

根据Ruby文档 :

 myTime = Time.at(1298011537289) 

或者因为你使用的是毫秒而不是秒:

 myTime = Time.at(1298011537289 / 1000) 

但这只会删除亚秒级精度,以保留它:

 myTime = Time.at(Rational(1298011537289, 1000)) 

使用strptime并使用格式%Q - Number of microseconds since 1970-01-01 00:00:00 UTC.解析它%Q - Number of microseconds since 1970-01-01 00:00:00 UTC.

示例: DateTime.strptime "1352748750274", "%Q"

假设该值以毫秒为单位,我就是这样做的:

 require 'date' DateTime.parse(Time.at(1298011537289 / 1000).to_s) # => #