Home > Software engineering >  Unix time to Date in R
Unix time to Date in R

Time:01-03

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:

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

CodePudding user response:

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"
  • Related