Unix Timestamp in Swift
Swift uses Date and TimeInterval (a Double of seconds) for timestamps. On Apple platforms, CFAbsoluteTime is relative to Jan 1 2001 — always use Date.timeIntervalSince1970 for Unix epoch compatibility.
Code Examples
Current timestamp (seconds)
let seconds = Int(Date().timeIntervalSince1970)
timeIntervalSince1970 returns a Double (seconds with sub-second precision). Cast to Int for a whole-number Unix timestamp.
Current timestamp (milliseconds)
let milliseconds = Int(Date().timeIntervalSince1970 * 1000)
Multiply by 1000 before casting to Int. Useful when sending timestamps to JavaScript or Java APIs that expect milliseconds.
Convert timestamp to Date
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
Construct a Date from a Unix timestamp. Pass a Double or TimeInterval — if you have seconds as Int, cast it first.
Format Date as string
let fmt = ISO8601DateFormatter() fmt.timeZone = TimeZone(identifier: "UTC") let str = fmt.string(from: date)
ISO8601DateFormatter produces RFC 3339 / ISO 8601 output. Always set timeZone explicitly to avoid local-time surprises.
Using Foundation — formatted output
let formatted = Date(timeIntervalSince1970: Double(timestamp))
.formatted(.dateTime.year().month().day().hour().minute().second().timeZone())Swift 5.5+ formatted API. More expressive than DateFormatter for display strings, though not suitable for machine parsing.
Note
Swift's Date is a thin wrapper around a Double of seconds since January 1, 2001 (not 1970). Always use timeIntervalSince1970 when you need a Unix timestamp. CFAbsoluteTime and Date() internally differ by 978,307,200 seconds — a common source of bugs when mixing Core Foundation and Swift APIs.
Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.
← Open the converter