iOS
Swift
Local Time
Date Timestamp
Programming

iOS Swift - Get the Current Local Time and Date Timestamp

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift, the current moment comes from Date(), but “local time” is a formatting concern rather than a different kind of timestamp. A Date represents an absolute instant, and you apply the user’s locale and time zone only when converting that instant into a readable string.

Get the Current Timestamp

If you want the current Unix timestamp, use Date directly:

swift
1import Foundation
2
3let now = Date()
4let seconds = now.timeIntervalSince1970
5let milliseconds = Int64(seconds * 1000)
6
7print(seconds)
8print(milliseconds)

This gives you an absolute time value independent of locale. That is usually what you want for storage, APIs, and comparisons.

Format the Current Local Date and Time

If you want a user-facing local date and time string, use DateFormatter with the current locale and time zone:

swift
1import Foundation
2
3let now = Date()
4let formatter = DateFormatter()
5formatter.locale = Locale.current
6formatter.timeZone = TimeZone.current
7formatter.dateStyle = .medium
8formatter.timeStyle = .medium
9
10let localString = formatter.string(from: now)
11print(localString)

This does not change the underlying timestamp. It only changes how the current moment is presented to the user.

That distinction is important because developers often talk about “local timestamp” when they really mean “localized display string.”

Use ISO 8601 When Interchanging Data

For APIs and logs, localized strings are often the wrong output because they depend on the user’s device settings. A stable format such as ISO 8601 is usually better:

swift
1import Foundation
2
3let now = Date()
4let formatter = ISO8601DateFormatter()
5let isoString = formatter.string(from: now)
6
7print(isoString)

This is especially useful when the timestamp will be parsed later by another service or compared across systems.

Calendar Components for Individual Parts

If you need hour, minute, day, or weekday values in local time, use Calendar with the current time zone:

swift
1import Foundation
2
3let now = Date()
4let calendar = Calendar.current
5let components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
6
7print(components.year ?? 0)
8print(components.hour ?? 0)

This is the right tool when you need logic based on local day boundaries or time-of-day rather than one formatted string.

Avoid Recreating Formatters Repeatedly in Hot Paths

DateFormatter is relatively expensive compared with simple arithmetic. If you are formatting dates frequently, reuse formatter instances instead of recreating them on every call.

For example:

swift
1import Foundation
2
3enum DateFormats {
4    static let localDateTime: DateFormatter = {
5        let formatter = DateFormatter()
6        formatter.locale = Locale.current
7        formatter.timeZone = TimeZone.current
8        formatter.dateStyle = .medium
9        formatter.timeStyle = .medium
10        return formatter
11    }()
12}
13
14print(DateFormats.localDateTime.string(from: Date()))

This matters in lists, logging systems, and repeated UI refreshes.

Common Pitfalls

The biggest mistake is thinking a Date itself is “local.” A Date is just a point in time. Locality appears when you format or decompose it.

Another issue is storing localized strings instead of timestamps. User-facing strings are poor interchange formats because they vary by locale and are harder to parse reliably.

Developers also sometimes confuse seconds and milliseconds since the Unix epoch. Many APIs expect one or the other, so be explicit about the unit.

Finally, avoid using fixed time-zone assumptions unless the product truly requires them. If the requirement is “current local time,” use TimeZone.current.

Summary

  • Use Date() to get the current moment.
  • Use timeIntervalSince1970 for an absolute timestamp.
  • Use DateFormatter and TimeZone.current for local display strings.
  • Use ISO 8601 for machine-readable interchange.
  • Remember that a Date is absolute; localization happens when you format it.

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.