How to convert NSDate into unix timestamp iphone sdk?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Converting NSDate into a Unix timestamp on iOS is straightforward because the Foundation date APIs already expose the number of seconds since January 1, 1970 UTC. The real detail is deciding whether you need whole seconds, milliseconds, or fractional seconds.
Use timeIntervalSince1970
NSDate represents an absolute point in time. To get the Unix timestamp, use timeIntervalSince1970.
In modern Swift with Date:
The result is a TimeInterval, which is a typealias for Double.
In Objective-C with NSDate:
The concept is the same even though the syntax is different.
Whole Seconds vs Fractional Seconds
Many APIs want whole Unix seconds, so you often cast the floating-point result to an integer.
Objective-C version:
If you still need sub-second precision, keep the floating-point value instead of truncating too early.
Milliseconds Are Also Common
Some backend systems use milliseconds rather than seconds. In that case, multiply by 1000.
Objective-C version:
Always confirm which unit the receiving system expects, because mixing seconds and milliseconds is one of the most common timestamp bugs.
NSDate and Date Are Bridged
Modern Swift code usually uses Date, but it bridges directly with NSDate.
So even if legacy code or older iPhone SDK examples use NSDate, the underlying conversion idea remains the same.
Time Zones Do Not Change the Timestamp
Unix timestamps represent absolute instants in UTC. The device time zone changes how the date is displayed, but it does not change the timestamp for a given moment.
That means the same instant displayed in Tokyo and New York still has the same Unix timestamp. This is exactly why timestamps are useful for storage and APIs.
Parse First, Then Convert
Often the input starts as a string from an API rather than as an NSDate. In that case, parse the string first and then convert the resulting date.
This is much safer than manually cutting apart a date string and guessing at the time zone rules.
Common Pitfalls
Forgetting that timeIntervalSince1970 returns seconds, not milliseconds, is the most common integration bug.
Converting to Int too early throws away sub-second precision that some APIs or logs may need.
Assuming the device time zone changes the Unix timestamp is incorrect because the timestamp always represents an absolute UTC moment.
Treating NSDate and Date as different conversion problems creates unnecessary complexity because they are bridged in Swift.
Parsing date strings manually instead of using a formatter often introduces subtle time-zone or format errors before the timestamp conversion even happens.
Summary
- Use
timeIntervalSince1970to convertNSDateorDateinto a Unix timestamp. - The raw result is in seconds as a floating-point value.
- Convert to
Intfor whole seconds or multiply by1000for milliseconds. - Time zones affect display, not the timestamp value of an instant.
- Parse incoming strings into a date object first, then convert to the timestamp you need.
Related reading
- How to convert this var string to URL in Swift
- How to convert this var string to URL in Swift
- How to convert UInt8 byte array to string in Swift
- How to Convert UIView to PDF within iOS?
- How to copy files from 'assets' folder to sdcard?
- How to copy files to Android emulator instance
- How to copy text programmatically in my Android app?
- How to Copy Text to Clipboard in Android?
.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.