NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to convert dates and times to a string using DateFormatter

Swift version: 5.2

Paul Hudson    @twostraws   

If you want to get a string from a Date, Apple’s DateFormatter class has everything you need: you can get short dates, long dates, dates with times, and can even go the opposite way to give you a Date from a string.

There are four primary ways you’re going to use it:

  1. Converting a Date instance to a string using one of the built-in date formats.
  2. Converting a Date instance to a string using one of the built-in time formats.
  3. Converting a Date instance to a string using a completely custom format.
  4. Converting a string instance to a Date.

Below are examples of each to get you started.

First, this converts a Date to a short date string using dateStyle:

let today = Date()
let formatter1 = DateFormatter()
formatter1.dateStyle = .short
print(formatter1.string(from: today))

That will print something like “12/31/2019” depending on the user’s locale.

Second, this converts the same date to a medium time string using timeStyle:

let formatter2 = DateFormatter()
formatter2.timeStyle = .medium
print(formatter2.string(from: today))

That will print something like “20:27:32” or “8:27:32pm” depending on the user’s locale.

Third, this converts the same date to a date and time string using a custom date format:

let formatter3 = DateFormatter()
formatter3.dateFormat = "HH:mm E, d MMM y"
print(formatter3.string(from: today))

That will print something like “20:32 Wed, 30 Oct 2019”.

Finally, this attempts to convert a string to a date

let string = "20:32 Wed, 30 Oct 2019"
let formatter4 = DateFormatter()
formatter4.dateFormat = "HH:mm E, d MMM y"
print(formatter4.date(from: string) ?? "Unknown date")

date(from:) returns an optional Date because it might be given a string containing an invalid value, so that code uses nil coalescing to make sure there’s a default value printed.

Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Subscribe to Hacking with Swift+

Available from iOS 2.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.9/5