Swift – Format Date into Different Styles
In this tutorial, we will learn how to format a date into different styles in Swift. We will use the DateFormatter
class to display dates in various predefined and custom formats. This tutorial includes practical examples and a complete program.
Formatting Dates in Swift
The DateFormatter
class in Swift provides an easy way to format Date
objects into human-readable strings. You can either use predefined styles or create custom formats by specifying the dateFormat
property.
Here’s how to format the current date:
import Foundation
let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let formattedDate = dateFormatter.string(from: currentDate)
print("Formatted Date: \(formattedDate)")
In this example, the date is displayed in the custom format yyyy-MM-dd HH:mm:ss
, such as 2024-11-19 15:45:29
.
Predefined Date Styles
The DateFormatter
class provides several predefined styles for dates and times. These styles are:
.short
: A concise style (e.g.,11/19/24
)..medium
: A moderately detailed style (e.g.,Nov 19, 2024
)..long
: A detailed style (e.g.,November 19, 2024
)..full
: The most detailed style (e.g.,Tuesday, November 19, 2024
).
Here’s how to use predefined styles:
dateFormatter.dateStyle = .short
print("Short Style: \(dateFormatter.string(from: currentDate))")
dateFormatter.dateStyle = .medium
print("Medium Style: \(dateFormatter.string(from: currentDate))")
dateFormatter.dateStyle = .long
print("Long Style: \(dateFormatter.string(from: currentDate))")
dateFormatter.dateStyle = .full
print("Full Style: \(dateFormatter.string(from: currentDate))")
Each style adjusts the level of detail displayed for the date.
Custom Date Formats
For custom date formats, use the dateFormat
property. The format strings are highly customizable, with common patterns including:
yyyy
: Year (e.g.,2024
)MM
: Month (e.g.,11
)dd
: Day (e.g.,19
)HH
: Hour in 24-hour format (e.g.,15
)hh
: Hour in 12-hour format (e.g.,03
)mm
: Minute (e.g.,45
)ss
: Second (e.g.,29
)a
: AM/PM marker (e.g.,PM
)
Here’s an example of custom formats:
dateFormatter.dateFormat = "yyyy-MM-dd"
print("Custom Format (yyyy-MM-dd): \(dateFormatter.string(from: currentDate))")
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
print("Custom Format (Day, Month d, Year): \(dateFormatter.string(from: currentDate))")
dateFormatter.dateFormat = "hh:mm a"
print("Custom Format (12-hour Time): \(dateFormatter.string(from: currentDate))")
dateFormatter.dateFormat = "HH:mm:ss"
print("Custom Format (24-hour Time): \(dateFormatter.string(from: currentDate))")
Complete Swift Program
Here’s the complete Swift program to demonstrate formatting dates into different styles:
import Foundation
// Get the current date
let currentDate = Date()
let dateFormatter = DateFormatter()
// Predefined Styles
dateFormatter.dateStyle = .short
print("Short Style: \(dateFormatter.string(from: currentDate))")
dateFormatter.dateStyle = .medium
print("Medium Style: \(dateFormatter.string(from: currentDate))")
dateFormatter.dateStyle = .long
print("Long Style: \(dateFormatter.string(from: currentDate))")
dateFormatter.dateStyle = .full
print("Full Style: \(dateFormatter.string(from: currentDate))")
// Custom Formats
dateFormatter.dateFormat = "yyyy-MM-dd"
print("Custom Format (yyyy-MM-dd): \(dateFormatter.string(from: currentDate))")
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
print("Custom Format (Day, Month d, Year): \(dateFormatter.string(from: currentDate))")
dateFormatter.dateFormat = "hh:mm a"
print("Custom Format (12-hour Time): \(dateFormatter.string(from: currentDate))")
dateFormatter.dateFormat = "HH:mm:ss"
print("Custom Format (24-hour Time): \(dateFormatter.string(from: currentDate))")
Output:
Short Style: 11/19/24
Medium Style: Nov 19, 2024
Long Style: November 19, 2024
Full Style: Tuesday, November 19, 2024
Custom Format (yyyy-MM-dd): 2024-11-19
Custom Format (Day, Month d, Year): Tuesday, Nov 19, 2024
Custom Format (12-hour Time): 03:45 PM
Custom Format (24-hour Time): 15:45:29
This program demonstrates how to format dates in various styles, including predefined and custom formats. You can easily adapt it to suit your specific needs.