user3206440 user3206440 - 1 year ago 175
Ruby Question

Ruby - epoch time with milliseconds to local time string

I have the following UNIX epoch timestamp -


I need to convert it into
date-time of format -
2016/09/28 00:01:13.536

Things I've tried

=> 48710-12-11 00:11:19 +0530 # gives a weird value for year

irb(main):010:0> Time.strptime(1475001600079.to_s, '%Q')
NoMethodError: undefined method `strptime' for Time:Class
from (irb):10
from C:/RailsInstaller/Ruby2.2.0/bin/irb:11:in `<main>'

How to go about this ?

RUby version is -
ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32]

Answer Source

Try / 1000.0).

The epoch time in ruby is in seconds, not milliseconds.

As to the NoMethodError, you need to require 'time' before you call Time::strptime.

Finally the format you need: / 1000.0).strftime('%Y/%m/%d %H:%M:%S.%3N')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download