iOS
Swift
NSDate
String Conversion
Code Example

Convert NSDate to String in iOS Swift

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a date to a string in Swift is a formatting task, not just a type cast. The standard tool is DateFormatter, and the important decisions are the output format, locale, and time zone you want the user or external system to see.

NSDate and Date

Modern Swift code usually works with Date, while older Cocoa APIs and Objective-C code often expose NSDate. The two bridge easily, so the formatting approach is the same in practice.

If you already have an NSDate, you can cast it to Date and format it:

swift
1import Foundation
2
3let nsDate = NSDate()
4let swiftDate = nsDate as Date

From that point on, use DateFormatter or ISO8601DateFormatter depending on the output you need.

Basic Conversion With DateFormatter

For user-facing strings, create a formatter, set the desired format or style, and ask it for a string.

swift
1import Foundation
2
3let now = Date()
4
5let formatter = DateFormatter()
6formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
7
8let text = formatter.string(from: now)
9print(text)

If you start from NSDate, the conversion is nearly identical:

swift
let legacyDate = NSDate()
let legacyText = formatter.string(from: legacyDate as Date)
print(legacyText)

This is the direct answer to the conversion problem, but correct formatting depends on context.

Prefer Styles for User-Facing Dates

Hard-coded date patterns are useful, but for many interfaces it is better to use formatter styles so the output respects the user's locale automatically.

swift
1let formatter = DateFormatter()
2formatter.dateStyle = .medium
3formatter.timeStyle = .short
4
5print(formatter.string(from: Date()))

This is often the right choice for labels, receipts, and app screens because it adapts to regional conventions instead of forcing one fixed pattern.

Set Locale and Time Zone Deliberately

Formatting bugs often come from default locale and time-zone behavior. If you are creating a display string for a person, the current locale is usually correct. If you are creating a machine-oriented string for logs or API payloads, use a fixed locale and time zone.

swift
1let formatter = DateFormatter()
2formatter.locale = Locale(identifier: "en_US_POSIX")
3formatter.timeZone = TimeZone(secondsFromGMT: 0)
4formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
5
6print(formatter.string(from: Date()))

The en_US_POSIX locale is commonly used for stable fixed-format strings because it avoids user-locale variations.

Use ISO8601DateFormatter for API-Like Strings

If the goal is an ISO 8601 timestamp rather than a custom display string, use the dedicated formatter.

swift
1import Foundation
2
3let isoFormatter = ISO8601DateFormatter()
4let isoText = isoFormatter.string(from: Date())
5print(isoText)

This is usually clearer and less error-prone than manually assembling an ISO pattern with DateFormatter.

Reuse Formatters When Performance Matters

DateFormatter creation is relatively expensive compared with ordinary value types. If you format many dates, do not build a new formatter every time inside a hot loop. Reuse one through a static property, a formatter helper, or a view model.

swift
1enum Formatters {
2    static let apiDate: DateFormatter = {
3        let formatter = DateFormatter()
4        formatter.locale = Locale(identifier: "en_US_POSIX")
5        formatter.timeZone = TimeZone(secondsFromGMT: 0)
6        formatter.dateFormat = "yyyy-MM-dd"
7        return formatter
8    }()
9}

This improves performance and keeps formatting rules consistent across the app.

Common Pitfalls

Using a fixed dateFormat for user-facing UI can create awkward results in different regions when a localized style would be better.

Forgetting to set locale and time zone for machine-readable strings can produce inconsistent output between devices.

Re-creating DateFormatter repeatedly in performance-sensitive code adds avoidable overhead.

Treating NSDate and Date as fundamentally different formatting problems is unnecessary because Swift bridges them cleanly.

Using a custom format when an ISO 8601 formatter is the real requirement often leads to fragile code.

Summary

  • Convert NSDate to String in Swift with DateFormatter or ISO8601DateFormatter.
  • 'NSDate and Date bridge cleanly, so the formatting workflow is the same.'
  • Use localized date and time styles for user-facing text.
  • Use fixed locale and time-zone settings for stable machine-readable strings.
  • Reuse formatter instances when formatting many dates.

Course illustration
Course illustration

All Rights Reserved.