Manipulating and formatting dates in Swift is a common task. This post presents examples for working with DateFormatter
, Date
, and other structs that enable parsing, formatting, and extracting components from dates and date strings.
- Date Formulas Cheatsheet
- Date Format Cheatsheet
- Format Date To String Using DateFormatter
a. timeStyle
b. dateStyle
c. locale
d. calendar - Calendar Cheatsheet
- Convert String To Date
- Date Components
a. Create Date From Date Components
b. Date Components From A Date
Date Formulas Cheatsheet
Formula | Output |
---|---|
dd.MM.yy | 16.01.23 |
MM/dd/yyyy | 01/16/2023 |
MM-dd-yyyy HH:mm | 01-16-2023 00:10 |
MMM d, h:mm a | Jan 16, 0:10 AM |
EEEE, MMM d, yyyy | Monday, Jan 16, 2023 |
yyyy-MM-dd’T’HH:mm:ssZ | 2023-01-16T00:10:00-0600 |
Date Format Cheatsheet
Formula | Description | Example |
---|---|---|
yyyy | 4-digit year | 2022 |
yy | 2-digit year | 22 |
MM | 2-digit month | 01 |
M | 1 or 2 digit month | 1 |
dd | 2-digit day of the month | 02 |
d | 1 or 2 digit day of the month | 2 |
HH | 2-digit hour (24-hour format) | 13 |
H | 1 or 2 digit hour (24-hour format) | 13 |
hh | 2-digit hour (12-hour format) | 01 |
h | 1 or 2 digit hour (12-hour format) | 1 |
mm | 2-digit minute | 02 |
m | 1 or 2 digit minute | 2 |
ss | 2-digit second | 02 |
s | 1 or 2 digit second | 2 |
a | AM/PM for 12-hour format | PM |
Format Date To String Using DateFormatter
The dateFormat
property on DateFormatter
specifies how the DateFormatter
instance will parse and format dates. After setting the dateFormat
to a format string, call date(from:)
with a formatted date string to parse the date string to a Date
:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let date = dateFormatter.date(from: "01/16/2023")
print(date) // Optional(2023-01-16 00:10:00 +0000)
timeStyle
DateFormatter
has another interface for specifying the time format of a date, the timeStyle
property. The timeStyle
property can be set to one of the following values:
Value | Description |
---|---|
.none | The time is not displayed. |
.short | The time is displayed in a short format, such as "0:10 AM". |
.medium | The time is displayed in a medium format, such as "0:10:00 AM". |
.long | The time is displayed in a long format, such as "0:10:00 AM EST". |
.full | The time is displayed in the most detailed format possible, including the time zone and seconds. |
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .medium
let timeString = dateFormatter.string(from: Date())
print(timeString) // "00:10:00 AM"
dateStyle
DateFormatter
also has an interface for specifying the date format of a date, the dateStyle
property. The dateStyle
property can be set to one of the following values:
Value | Description |
---|---|
.none | The date is not displayed. |
.short | The date is displayed in a short format, such as "1/16/2023". |
.medium | The date is displayed in a medium format, such as "Jan 16, 2023". |
.long | The date is displayed in a long format, such as "January 16, 2023". |
.full | The date is displayed in the most detailed format possible, including the weekday and era. |
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
let dateString = dateFormatter.string(from: Date())
print(dateString) // "Jan 16, 2023"
locale
To control the language and region-specific information in a formatted date, use the locale property on DateFormatter
.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy"
dateFormatter.locale = Locale(identifier: "fr_FR")
let date = dateFormatter.string(from: Date())
print(date) // "janvier 16, 2023"
To set the locale property to the user's device settings, use .autoupdatingCurrent
:
dateFormatter.locale = Locale.autoupdatingCurrent
calendar
To control the calendar used to format the Date
, use the calendar
property on DateFormatter
.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy"
dateFormatter.calendar = Calendar(identifier: .islamic)
let date = dateFormatter.string(from: Date())
print(date) // "Jumada II 22, 1444"
Calendar Cheatsheet
Calendar | Description |
---|---|
gregorian | The Gregorian calendar, used in most of the world. |
buddhist | The Buddhist calendar, used in Thailand and other countries with a Buddhist majority. |
chinese | The Chinese calendar, used in China and other countries with a significant Chinese population. |
coptic | The Coptic calendar, used in the Coptic Orthodox Church. |
ethiopicAmeteAlem | The Ethiopian calendar, also known as the Ethiopian Orthodox Tewahedo Church calendar. |
ethiopicAmeteMihret | The Ethiopian calendar, also known as the Ethiopian Orthodox Tewahedo Church calendar. |
hebrew | The Hebrew calendar, used in Israel and other Jewish communities. |
iso8601 | The ISO 8601 calendar, an international standard for date and time representation. |
indian | The Indian national calendar, used in India and other countries with a significant Indian population. |
islamic | The Islamic calendar, used in Muslim communities around the world. |
islamicCivil | The Islamic calendar, used in Muslim communities around the world. |
japanese | The Japanese calendar, used in Japan. |
persian | The Persian calendar, used in Iran and other countries with a significant Persian population. |
roc | The Republic of China calendar, used in Taiwan and other Chinese communities. |
Convert String To Date
In addition to formatting dates and times, use DateFormatter
to parse a String
into a Date
. Just like formatting dates, set dateFormat
on the DateFormatter
to the format of the date string. Then, call date(from:)
passing in a date string to get a parsed Date
:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateString = "2023-01-16"
let date = dateFormatter.date(from: dateString)
print(date) // Optional(2023-01-16 00:10:00 +0000)
Date Components
DateComponents
is a struct that enables working with specific components of a date, like year, month, or day.
Create Date From Date Components
To create a Date
in Swift by specifying each individual date component, use DateComponents
:
var components = DateComponents()
components.day = 15
components.month = 1
components.year = 2023
components.hour = 0
components.minute = 10
let date = Calendar.current.date(from: components)
Date Components From A Date
To obtain date components, like the day number, from a Date
use DateComponents
:
// Use the user's set calendar
let calendar = Calendar.autoupdatingCurrent
// Get the year, month, and day from the date
let components = calendar.dateComponents(
[.year, .month, .day],
from: Date()
)
// Reference the date components
nowComponents.year
nowComponents.month
nowComponents.day
Parsing and Formatting Dates In Swift
That's it! By using DateFormatter
, Date
, Calendar
, and DateComponents
you can parse, format, and work with date components in Swift.