SiH
SiH

Reputation: 1546

Convert numeric time in milliseconds to datetime with seconds with decimal value

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

time = 1633708877772

Upvotes: 2

Views: 895

Answers (1)

akrun
akrun

Reputation: 886938

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"

Upvotes: 2

Related Questions