For Developers

Unix Timestamp in Swift

How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in Swift — plus the reference-date offset that catches iOS developers off guard.

let ts = Date().timeIntervalSince1970 // Double, seconds
let seconds: TimeInterval = 1750000000
let date = Date(timeIntervalSince1970: seconds)
var comps = DateComponents()
comps.year = 2025; comps.month = 6; comps.day = 15
comps.hour = 12; comps.minute = 0; comps.second = 0
var utc = Calendar(identifier: .gregorian)
utc.timeZone = TimeZone(identifier: "UTC")!
let date = utc.date(from: comps)!
let ts = date.timeIntervalSince1970

The Swift-specific pitfall

Swift's Date type doesn't actually store a Unix timestamp internally — it stores seconds relative to January 1, 2001 (the "reference date"), a Cocoa/Foundation convention inherited from Objective-C. timeIntervalSince1970 handles the conversion for you, but if you ever see a raw number from timeIntervalSinceReferenceDate or a Core Data field and treat it as a Unix timestamp, it will be off by exactly 978,307,200 seconds — the gap between 1970 and 2001. It's a specific, memorable offset, and worth knowing on sight rather than debugging from scratch.

Have a timestamp right now?

Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.

Related

Related tools & reading