Get Unix Epoch Time in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, the standard way to get Unix epoch time is through Date and its timeIntervalSince1970 property. That value represents the number of seconds since 1970-01-01 00:00:00 UTC. The main follow-up decision is whether you want fractional seconds as a Double, whole seconds as an integer, or milliseconds for APIs that use a larger unit.
The Basic Swift API
The direct solution is short:
timeIntervalSince1970 returns a TimeInterval, which is just a typealias for Double. That means you get sub-second precision.
Example output might look like:
This is usually what you want when precision matters.
Whole Seconds As An Integer
If you need a Unix timestamp as a whole number, convert it explicitly.
This truncates the fractional part. That is a common requirement for:
- REST APIs expecting integer timestamps
- database fields stored as seconds
- log formats using integer Unix time
Be aware that this is truncation, not rounding.
Milliseconds Since Epoch
Some APIs expect milliseconds instead of seconds. Multiply before converting.
That pattern is common when interacting with JavaScript systems, analytics pipelines, or storage layers that use millisecond timestamps.
Convert A Specific Date To Unix Time
You are not limited to the current time. Any Date can be converted to epoch seconds.
This is useful when parsing external timestamps and then normalizing them into Unix time for comparison or storage.
Convert Unix Time Back To Date
The inverse operation is just as important.
For milliseconds, divide by 1000 first:
Keeping the unit straight is the critical part.
Time Zones Do Not Change The Timestamp
Unix epoch time is based on UTC. The numeric timestamp represents an absolute moment in time, not a local clock display. Local time zones only affect how you format or display the Date later.
That value is the same absolute instant regardless of whether the phone is in Toronto, London, or Tokyo. If the displayed date string changes, that is a formatting concern, not a timestamp concern.
Choosing The Right Numeric Type
For current timestamps, Int is usually fine on modern Apple platforms. But if you care about precision or cross-platform serialization, keep a few rules in mind:
- use
Doublewhen fractional seconds matter - use
Intwhen an API expects whole seconds or milliseconds - document the unit clearly so seconds are not mistaken for milliseconds later
A lot of timestamp bugs are really unit bugs.
Common Pitfalls
- Forgetting that
timeIntervalSince1970returns seconds, not milliseconds. - Converting to
Inttoo early and unintentionally losing fractional precision. - Mixing up milliseconds and seconds when sending data to external APIs.
- Assuming local time zone changes the epoch number itself.
- Parsing or formatting a
Dateincorrectly and blaming the Unix timestamp logic.
Summary
- In Swift, get Unix epoch time with
Date().timeIntervalSince1970. - The result is a
Doublein seconds since the Unix epoch. - Convert to
Intif you need whole seconds. - Multiply by
1000if an API expects milliseconds. - Time zones affect display formatting, not the epoch timestamp value itself.
Related reading
- Get Value of a Edit Text field
- Get visible items in RecyclerView
- getActivity returns null in Fragment function
- getApplication vs. getApplicationContext
- getColorint id deprecated on Android 6.0 Marshmallow API 23
- Get/pick an image from Android's built-in Gallery app programmatically
- getResources.getColor is deprecated
- getString Outside of a Context or Activity
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.