Swift DateFormatter Cheatsheet [+Formulas, Parsing Dates]

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.

  1. Date Formulas Cheatsheet
  2. Date Format Cheatsheet
  3. Format Date To String Using DateFormatter
    a. timeStyle
    b. dateStyle
    c. locale
    d. calendar
  4. Calendar Cheatsheet
  5. Convert String To Date
  6. Date Components
    a. Create Date From Date Components
    b. Date Components From A Date

Date Formulas Cheatsheet

FormulaOutput
dd.MM.yy16.01.23
MM/dd/yyyy01/16/2023
MM-dd-yyyy HH:mm01-16-2023 00:10
MMM d, h:mm aJan 16, 0:10 AM
EEEE, MMM d, yyyyMonday, Jan 16, 2023
yyyy-MM-dd’T’HH:mm:ssZ2023-01-16T00:10:00-0600

Date Format Cheatsheet

FormulaDescriptionExample
yyyy4-digit year2022
yy2-digit year22
MM2-digit month01
M1 or 2 digit month1
dd2-digit day of the month02
d1 or 2 digit day of the month2
HH2-digit hour (24-hour format)13
H1 or 2 digit hour (24-hour format)13
hh2-digit hour (12-hour format)01
h1 or 2 digit hour (12-hour format)1
mm2-digit minute02
m1 or 2 digit minute2
ss2-digit second02
s1 or 2 digit second2
aAM/PM for 12-hour formatPM

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:

ValueDescription
.noneThe time is not displayed.
.shortThe time is displayed in a short format, such as “0:10 AM”.
.mediumThe time is displayed in a medium format, such as “0:10:00 AM”.
.longThe time is displayed in a long format, such as “0:10:00 AM EST”.
.fullThe 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:

ValueDescription
.noneThe date is not displayed.
.shortThe date is displayed in a short format, such as “1/16/2023”.
.mediumThe date is displayed in a medium format, such as “Jan 16, 2023”.
.longThe date is displayed in a long format, such as “January 16, 2023”.
.fullThe 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

CalendarDescription
gregorianThe Gregorian calendar, used in most of the world.
buddhistThe Buddhist calendar, used in Thailand and other countries with a Buddhist majority.
chineseThe Chinese calendar, used in China and other countries with a significant Chinese population.
copticThe Coptic calendar, used in the Coptic Orthodox Church.
ethiopicAmeteAlemThe Ethiopian calendar, also known as the Ethiopian Orthodox Tewahedo Church calendar.
ethiopicAmeteMihretThe Ethiopian calendar, also known as the Ethiopian Orthodox Tewahedo Church calendar.
hebrewThe Hebrew calendar, used in Israel and other Jewish communities.
iso8601The ISO 8601 calendar, an international standard for date and time representation.
indianThe Indian national calendar, used in India and other countries with a significant Indian population.
islamicThe Islamic calendar, used in Muslim communities around the world.
islamicCivilThe Islamic calendar, used in Muslim communities around the world.
japaneseThe Japanese calendar, used in Japan.
persianThe Persian calendar, used in Iran and other countries with a significant Persian population.
rocThe 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 DateFormatterDateCalendar, and DateComponents you can parse, format, and work with date components in Swift.