How to convert a Unix timestamp to POSIXct in R (i.e. 1532289300 = 2018-07-22 19:55:00)

If you are converting a column of Unix timestamps in a dataframe:

> df$Your.Times <- as.POSIXct(df$Your.Times, origin = “1970-1-1”)
[1] [POSIXct datetimes]

… or …

If you are converting a single Unix timestamp:

> yourtime = as.POSIXct(1532289300, origin = “1970-1-1”)
[1] 2018-07-22 19:55:00

Note: These solutions require the lubridate package.

Explanation:

In brief, as understood by Unix, time started on January 1, 1970 (the epoch date). The epoch date is represented as “0” Coordinated Universal Time (“UTC”) in Unix systems and all datetimes after the epoch date are represented as the number of seconds that have passed since the epoch date. Therefore, a Unix timestamp is the number of seconds since January 1, 1970 … approximately … leap seconds are not counted. as.POSIXct works be converting the number of seconds since January 1, 1970 into a readable datetime.

 
1
Kudos
 
1
Kudos

Now read this

How to identify the length of time between two dates in R

If you are finding the length of time (in hours) between two columns of dates in a dataframe: > df$yourLength <- round(int_length(interval( df$yourStartTime, df$yourEndTIme)) / 60 / 60, digits = 2) [1] [interval lengths] … or … If... Continue →