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.
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:
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:
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:
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:
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:
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
timeIntervalSince1970for an absolute timestamp. - Use
DateFormatterandTimeZone.currentfor local display strings. - Use ISO 8601 for machine-readable interchange.
- Remember that a
Dateis absolute; localization happens when you format it.
Related reading
- ios swift parse methods with async results
- iOS Swift remove elements of an array from another array
- iOS Tests/Specs TDD/BDD and Integration Acceptance Testing
- iOS UI Testing On an Isolated View
- iOS UIButton resize according to text length
- iOS UIImagePickerController result image orientation after upload
- iOS UITextView or UILabel with clickable links to actions
- iOS unique user identifier
.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.