Unix Timestamp in Swift
Swift's Date stores a TimeInterval relative to 2001, not 1970, so always go through the explicit epoch initialisers.
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
import Foundation
let secs = Int(Date().timeIntervalSince1970) // 1710050400
let ms = Int(Date().timeIntervalSince1970 * 1000) Timestamp to date
let date = Date(timeIntervalSince1970: 1710050400)
let fmt = DateFormatter()
fmt.timeZone = TimeZone(identifier: "Asia/Kolkata")
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
print(fmt.string(from: date))
let iso = ISO8601DateFormatter()
print(iso.string(from: date)) Date to timestamp
var comps = DateComponents()
comps.year = 2024; comps.month = 3; comps.day = 10
comps.hour = 11; comps.minute = 30
var cal = Calendar(identifier: .gregorian)
cal.timeZone = TimeZone(identifier: "Asia/Kolkata")!
if let d = cal.date(from: comps) {
print(Int(d.timeIntervalSince1970)) // 1710050400
} Beware the 2001 epoch
// timeIntervalSinceReferenceDate counts from 2001-01-01, not 1970.
Date().timeIntervalSinceReferenceDate // ~760,000,000 — NOT a Unix timestamp
Date().timeIntervalSince1970 // ~1,710,000,000 — this one is Pitfalls specific to Swift
- Apple's reference date is 1 January 2001. Mixing up
timeIntervalSinceReferenceDateandtimeIntervalSince1970shifts every date by 31 years. DateFormatteris expensive to construct — cache it, or performance falls off a cliff in list rendering.
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
- Kotlin integer milliseconds
- C time_t seconds
- Perl integer seconds
- Dart integer milliseconds
- PowerShell .NET DateTimeOffset
- Scala integer milliseconds
- R float seconds
- Excel days since 1899-12-30
Frequently asked questions
How do I get the current Unix timestamp in Swift?
Use the snippet in the "Current Unix timestamp" section above. Swift 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 Swift?
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 Swift 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 Swift makes that easy to get wrong.