Converting a date to a string isn't difficult in Swift. The class that makes this task painless is DateFormatter
(or NSDateFormatter
if you are using Objective-C). The DateFormatter
class is easy to use. Let me show you an example using a playground.
We add an import statement for Foundation because the Date
struct and the DateFormatter
class are defined in Foundation.
import Foundation
We start by creating a Date
object. To convert the date to a string, we need to create a date formatter, an instance of the DateFormatter
class.
import Foundation
// Create Date
let date = Date()
// Create Date Formatter
let dateFormatter = DateFormatter()
To convert the Date
object to a string, we invoke the date formatter's string(from:)
method. The string(from:)
method accepts a Date
object as its only argument and returns a String
object. That is what we want. Right?
import Foundation
// Create Date
let date = Date()
// Create Date Formatter
let dateFormatter = DateFormatter()
// Convert Date to String
dateFormatter.string(from: date)
Something isn't quite right, though. The string(from:)
method returns an empty string.
We forgot one important detail. We haven't told the date formatter what format the date should be in. We need to set the dateFormat
property of the DateFormatter
instance. Let's give it another try.
import Foundation
// Create Date
let date = Date()
// Create Date Formatter
let dateFormatter = DateFormatter()
// Set Date Format
dateFormatter.dateFormat = "YY/MM/dd"
// Convert Date to String
dateFormatter.string(from: date)
This looks much better.
The dateFormat
property of the date formatter is of type String
. How do you know what the value of dateFormat
should be? The value you assign to the dateFormat
property needs to conform to UTS or Unicode Technical Standard. While this may sound complex, it really isn't. Take a look at these examples.
"y, M d" // 2023, 4 19
"YY, MMM d" // 23, Apr 19
"YY, MMM d, hh:mm" // 23, Apr 19, 02:18
"YY, MMM d, HH:mm:ss" // 23, Apr 19, 14:18:31
Date Style and Time Style
You are not required to set the dateFormat
property. You can simplify the implementation by setting the dateStyle
and timeStyle
properties. Both properties are of type DateFormatter.Style
, an enum that defines a handful of cases. This option offers less flexibility, but it may be sufficient for your needs.
extension DateFormatter {
public enum Style : UInt {
case none = 0
case short = 1
case medium = 2
case long = 3
case full = 4
}
}
Take a look at this example in which we set dateStyle
to long
and timeStyle
to short
.
import Foundation
// Create Date
let date = Date()
// Create Date Formatter
let dateFormatter = DateFormatter()
// Set Date/Time Style
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .short
// Convert Date to String
dateFormatter.string(from: date) // April 19, 2023 at 4:42 PM
Scratching the Surface
The DateFormatter
class has many more options. For example, you can set the locale to localize the string the date formatter returns.
import Foundation
// Create Date
let date = Date()
// Create Date Formatter
let dateFormatter = DateFormatter()
// Set Date/Time Style
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .short
// Set Locale
dateFormatter.locale = Locale(identifier: "fr")
// Convert Date to String
dateFormatter.string(from: date) // 19 avril 2023 à 16:42
I encourage you to explore the documentation of the DateFormatter
class to explore the other options this API has to offer.