How can I convert a tick, such as 1298011537289
to a DateTime in Ruby?
The value I need to convert is coming from a JavaScript Date.now()
call, so it's in milliseconds
Per the Ruby docs:
myTime = Time.at(1298011537289)
or since you're using milliseconds rather than seconds:
myTime = Time.at(1298011537289 / 1000)
But that will only remove sub-second precision, to retain it:
myTime = Time.at(Rational(1298011537289, 1000))
Use strptime and parse it with the format %Q - Number of microseconds since 1970-01-01 00:00:00 UTC.
Example:
DateTime.strptime "1352748750274", "%Q"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With