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.

Frequently Asked Questions

How do I get the current Unix timestamp in Swift?

Use Date().timeIntervalSince1970, which returns a Double representing seconds since January 1, 1970 UTC. Cast to Int for a whole-number Unix timestamp: let seconds = Int(Date().timeIntervalSince1970). No imports needed — Date is part of Foundation.

Why doesn't Swift's Date() directly give a Unix timestamp?

Swift's Date internally stores time as seconds since January 1, 2001 (Core Data epoch), not 1970. Always use .timeIntervalSince1970 to get Unix time. Using .timeIntervalSinceReferenceDate would give a value that's 978,307,200 seconds off — a common source of hard-to-spot bugs.

How do I get milliseconds in Swift?

Multiply timeIntervalSince1970 by 1000 before casting: let ms = Int(Date().timeIntervalSince1970 * 1000). This is needed when sending timestamps to JavaScript (Date.now()), Java (System.currentTimeMillis()), or any API that expects 13-digit millisecond timestamps.

How do I convert a Unix timestamp back to a Date in Swift?

Use Date(timeIntervalSince1970: TimeInterval(timestamp)). If you have an Int, cast it to Double first: Date(timeIntervalSince1970: Double(myIntTimestamp)). You can then format it with ISO8601DateFormatter or DateFormatter.

Should I use Int or Double for Unix timestamps in Swift?

Use Int for storing and transmitting timestamps — it's unambiguous and matches how most APIs and databases represent epoch seconds. Use Double (TimeInterval) for intermediate calculations involving sub-second precision, such as measuring durations or working with sub-second timestamps.

Main Epoch Converter

Use the homepage when you need epoch to date, date to epoch, or a full epoch converter

This Swift guide shows how to create and parse Unix timestamps in code. If your next step is broader search intent like epoch converter, epoch to date, epoch time to date, or unix epoch converter, jump back to the live homepage tools to paste a raw Unix value, auto-detect seconds versus milliseconds, and copy the readable answer instantly.

Related Tools

More free tools for developers debugging timestamps, schedules, and text changes.