Home > OS >  Convert numeric time in milliseconds to datetime with seconds with decimal value
Convert numeric time in milliseconds to datetime with seconds with decimal value

Time:11-17

How to convert numeric time in milliseconds to datetime format with seconds with decimal values (preferably using lubridate?

time = 1633708877772

CodePudding user response:

With lubridate, using as_datetime

library(lubridate)
as_datetime(time/1000)
[1] "2021-10-08 16:01:17 UTC"

Note that the milliseconds are not printed in the console. If we need to print, then format with strftime or format (but it will not be a datetime object anymore)

strftime(as_datetime(time/1000), '%Y-%m-%d %H:%M:%OS3') 
#[1] "2021-10-08 11:01:17.772"

Or without using any package, just specify it in as.POSIXct

as.POSIXct(time/1000, origin = '1970-01-01')
[1] "2021-10-08 11:01:17 CDT"
  • Related