NSDate
unix timestamp
iPhone SDK
conversion
Swift programming

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.

Browse interview questions

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:

swift
1import Foundation
2
3let now = Date()
4let seconds = now.timeIntervalSince1970
5
6print(seconds)

The result is a TimeInterval, which is a typealias for Double.

In Objective-C with NSDate:

objective-c
NSDate *now = [NSDate date];
NSTimeInterval seconds = [now timeIntervalSince1970];
NSLog(@"%f", seconds);

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.

swift
let timestamp = Int(Date().timeIntervalSince1970)
print(timestamp)

Objective-C version:

objective-c
NSInteger timestamp = (NSInteger)[[NSDate date] timeIntervalSince1970];
NSLog(@"%ld", (long)timestamp);

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.

swift
let milliseconds = Int(Date().timeIntervalSince1970 * 1000)
print(milliseconds)

Objective-C version:

objective-c
long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
NSLog(@"%lld", milliseconds);

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.

swift
let nsDate: NSDate = NSDate()
let timestamp = nsDate.timeIntervalSince1970
print(timestamp)

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.

swift
1let formatter = ISO8601DateFormatter()
2let date = formatter.date(from: "2025-09-23T14:28:25Z")!
3let timestamp = Int(date.timeIntervalSince1970)
4print(timestamp)

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 timeIntervalSince1970 to convert NSDate or Date into a Unix timestamp.
  • The raw result is in seconds as a floating-point value.
  • Convert to Int for whole seconds or multiply by 1000 for 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.