Unix Timestamp in R
R's POSIXct is literally a Unix timestamp stored as a double, with the time zone carried as an attribute.
Need to convert one value rather than write code? Use the interactive converter — it handles every unit and time zone without leaving your browser.
Current Unix timestamp
as.numeric(Sys.time()) # 1710050400.123
as.integer(Sys.time()) # 1710050400 Timestamp to date
ts <- 1710050400
as.POSIXct(ts, origin = "1970-01-01", tz = "UTC")
format(as.POSIXct(ts, origin = "1970-01-01", tz = "Asia/Kolkata"),
"%Y-%m-%d %H:%M:%S %Z")
# lubridate
library(lubridate)
as_datetime(ts, tz = "UTC") Date to timestamp
d <- as.POSIXct("2024-03-10 06:00:00", tz = "UTC")
as.numeric(d) # 1710050400
library(lubridate)
as.numeric(ymd_hms("2024-03-10 11:30:00", tz = "Asia/Kolkata")) Whole columns
df$datetime <- as.POSIXct(df$epoch, origin = "1970-01-01", tz = "UTC")
# Milliseconds
df$datetime <- as.POSIXct(df$epoch_ms / 1000, origin = "1970-01-01", tz = "UTC") Pitfalls specific to R
as.POSIXcton a numeric requiresoriginbefore R 4.3, and errors without it. Passing it is harmless on newer versions.- R prints
POSIXctin the session time zone, so the printed value can differ from what is stored. SetSys.setenv(TZ = "UTC")for reproducibility.
Rules that apply in every language
- Store UTC, display local. Keep the instant in UTC everywhere in your system and convert only at the point a human reads it.
- Name the unit in the identifier.
expires_at_msrather thanexpires_atcosts nothing and prevents the single most common timestamp bug. - Never trust a client clock. Stamp anything security-relevant on the server. See the note on clock skew.
- Use 64-bit time. Anything still storing seconds in a signed 32-bit field breaks in January 2038 — see the Year 2038 problem.
The same task in other languages
- Python float seconds
- JavaScript integer milliseconds
- PHP integer seconds
- Java integer milliseconds
- Go integer seconds and nanoseconds
- TypeScript integer milliseconds
- Ruby float seconds
- C# ticks (100 ns)
- Rust seconds + nanoseconds
- SQL varies by engine
- Bash integer seconds
- C++ chrono duration
- Swift float seconds
- Kotlin integer milliseconds
- C time_t seconds
- Perl integer seconds
- Dart integer milliseconds
- PowerShell .NET DateTimeOffset
- Scala integer milliseconds
- Excel days since 1899-12-30
Frequently asked questions
How do I get the current Unix timestamp in R?
Use the snippet in the "Current Unix timestamp" section above. R works in float seconds natively, so converting to another unit is a multiplication or an integer division away.
How do I convert a Unix timestamp to a date in R?
The "Timestamp to date" snippet above shows the idiomatic approach, including how to render the result in a specific time zone rather than whatever zone the machine happens to be set to.
Does R handle time zones and daylight saving correctly?
Yes, provided you pass an explicit zone rather than relying on the system default. The gotchas listed on this page cover the specific ways R makes that easy to get wrong.