Swift:
Parsing a date from a string
How to:
Using Foundation’s DateFormatter
Swift’s standard library, Foundation, provides DateFormatter
for converting strings to Date
objects and vice versa. To parse a date from a string, you specify the date format that matches the string, then use the formatter to parse it.
import Foundation
let dateString = "2023-04-30"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
if let date = formatter.date(from: dateString) {
print("Parsed date: \(date)")
} else {
print("Failed to parse date")
}
// Sample Output: Parsed date: 2023-04-29 22:00:00 +0000
Note that the output may vary based on your timezone.
Using ISO8601DateFormatter
For ISO 8601 date formats, Swift provides a specialized formatter, ISO8601DateFormatter
, which simplifies the parsing process.
import Foundation
let dateString = "2023-04-30T15:00:00+00:00"
let isoFormatter = ISO8601DateFormatter()
if let date = isoFormatter.date(from: dateString) {
print("Parsed ISO8601 date: \(date)")
} else {
print("Failed to parse ISO8601 date")
}
// Sample Output: Parsed ISO8601 date: 2023-04-30 15:00:00 +0000
Using a Third-Party Library: SwiftDate
While Swift provides robust tools for date parsing, third-party libraries like SwiftDate offer even more flexibility and convenience. After adding SwiftDate to your project, parsing becomes as simple as:
import SwiftDate
let dateString = "April 30, 2023"
if let date = dateString.toDate("MMMM dd, yyyy") {
print("Parsed date with SwiftDate: \(date)")
} else {
print("Failed to parse date with SwiftDate")
}
// Sample Output: Parsed date with SwiftDate: 2023-04-30 00:00:00 +0000
SwiftDate simplifies parsing with natural language and a wide range of date formats, making it a powerful addition to your Swift programming toolkit.