Unix Timestamp in R
R stores dates as numeric values internally. Use Sys.time() to get the current timestamp and as.numeric() to convert to a Unix epoch in seconds. The lubridate package (tidyverse) simplifies date arithmetic significantly.
Code Examples
Current timestamp (seconds)
as.numeric(Sys.time())
Sys.time() returns a POSIXct object. Wrapping it in as.numeric() extracts the Unix timestamp in seconds (double-precision, so fractional seconds are preserved).
Current timestamp (milliseconds)
as.numeric(Sys.time()) * 1000
Multiply by 1000 and use floor() or as.integer() if you need an integer millisecond value. R's POSIXct stores time as fractional seconds internally.
Convert timestamp to POSIXct
as.POSIXct(1708560000, origin = "1970-01-01", tz = "UTC")
Reconstruct a POSIXct date-time from a Unix timestamp. Always specify tz = "UTC" to avoid implicit local-timezone conversion.
Format timestamp as string
format(as.POSIXct(1708560000, origin="1970-01-01", tz="UTC"), "%Y-%m-%d %H:%M:%S")
format() applies strftime-style format strings to POSIXct objects. Combine with as.POSIXct to go from Unix timestamp to human-readable string.
Using lubridate (tidyverse)
library(lubridate) ts <- as_datetime(1708560000) class(ts) # POSIXct
lubridate's as_datetime() assumes UTC and is more readable than base R. It returns a POSIXct object compatible with ggplot2 and dplyr workflows.
Note
R's POSIXct class stores time as seconds since 1970-01-01 UTC — the same as Unix epoch. The key gotcha is that R's as.POSIXct() uses the local timezone by default if you don't specify tz = "UTC". Always be explicit to avoid off-by-hours bugs in data analysis.
Related Languages
Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.
← Open the converter