Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unix time to Date in R

I need to convert a vector of dates from Unix Time to Date format using R. For more context, this dates come from Sentinel-2 satellite imagery time series with one image every five days. The time series starts on 2020/01/01.

dates <- c(1577876409428, 1578308409796, 1578740409279, 1579172409017, 1579604408531)

I’m trying to convert to date using as follows:

library(lubridate)
new_date <- as.Date(as.POSIXct(dates, origin="1970-01-01"))

However, I get wrong results:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"51970-11-30" "51984-08-08" "51998-04-17" "52011-12-25" "52025-09-02"

>Solution :

Divide by 1000 and it should work

as.Date(as.POSIXct(dates/1000, origin="1970-01-01"))
[1] "2020-01-01" "2020-01-06" "2020-01-11" "2020-01-16" "2020-01-21"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading