Get Year, Month or Day From Date In Swift - Tutorial

By Eddy Chung

Let’s get the components we need from a date in Swift.

Get Year From Date In Swift

To get the year as a string from a date, use DateFormatter:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy"
let yearString = dateFormatter.string(from: date)

This will print a 4 digit year.

If you just want two digits, use yy instead of yyyy.

To get the year as an Int from a date, use calendar.dateComponents:

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year], from: date)
let year = components.year

Get Month From Date In Swift

To get the name of the month, use DateFormatter:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let monthString = dateFormatter.string(from: date)

This will give you a string with the full month name.

To get the month as an Int, use calendar.dateComponents:

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.month], from: date)
let month = components.month

Get Day Of The Week From Date In Swift

To get the day of the week from a date in Swift, use DateFormatter:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let dayOfTheWeekString = dateFormatter.string(from: date)

This will give you the full name of the day of the week, such as “Monday”.

If you want to get the number 1-7, with 1 representing Sunday and 7 representing Saturday, use dateComponents:

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.weekday], from: date)
let dayOfWeek = components.weekday

Get Day Of The Month

To get the day of the month, you can use dateComponents:

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.day], from: date)
let dayOfMonth = components.day

Other Date Formats

If you’d like the date formatter in a another way that wasn’t covered above, you can check out this table for DateFormatter:

Characters Example Explained
Year
y 2019 Year with no padding
yy 19 Year with two digits, will pad with zero if needed
yyyy 2019 Year, with four digits, will pad with zero if needed
Month
M 1 Numeric month with no padding
MM 01 Two digit numeric month with padding
MMMM January Full name of the month
Day
d 1 Day of the month
dd 01 Two digit day of the month with padding
E Fri Day of the week short-hand
EEEE Friday Full name of the day
Hour
h 2 Twelve hour format
hh 02 Twelve hour format with padding
H 14 24 hour format
HH 04 24 hour format with padding
a AM AM / PM
Minute
m 30 Minute with no padding
mm 35 Minutes with padding
Seconds
s 5 Seconds with no padding
ss 05 Seconds with padding
SSS 4321 Milliseconds
Time Zones
zzz PST 3 letter timezone
zzzz Pacific Standard Time The whole timezone name
Z -0800 RFC 822 GMT Format
ZZZZZ -08:00 ISO 8601 Format

Compare And Check Dates

In this section, we’ll go over how to compare dates.

Check If From Today

Calendar has the method func isDateInToday(_ date: Date) -> Bool we can utilize.

let date = Date()
print(Calendar.current.isDateInToday(date))

This will print true.

Check If From Tomorrow

Calendar has the method func isDateInTomorrow(_ date: Date) -> Bool we can utilize.

let date = Date()
print(Calendar.current.isDateInTomorrow(date))

This will print false, since Date() creates a date that is in this instant and therefore today.

Check If From Yesterday

Calendar has the method func isDateInYesterday(_ date: Date) -> Bool we can utilize.

let date = Date()
print(Calendar.current.isDateInYesterday(date))

This will print false, since Date() creates a date that is in this instant and therefore today.

Check If From Weekend

Calendar has the method func isDateInWeekend(_ date: Date) -> Bool we can utilize.

let date = Date()
print(Calendar.current.isDateInWeekend(date))

The output of this will depend if you run this code on the weekend or not :)

Check If From The Same Day

Calendar has the method func isDate(_ date: Date, inSameDayAs: Date) -> Bool we can utilize.

let date1 = Date()
let date2 = Date()

print(Calendar.current.isDate(date1, inSameDayAs: date2))

This will print true.

Check If From The Same Week

Calendar has the method func isDate(_ date: Date, equalTo: Date, toGranularity: DateComponents) -> Bool we can utilize.

For the same week, we can set the toGranularity to .weekOfYear:

let date1 = Date()
let date2 = Date()

print(Calendar.current.isDate(date1, equalTo: date2, toGranularity: .weekOfYear))

Check If From This Month

Calendar has the method func isDate(_ date: Date, equalTo: Date, toGranularity: DateComponents) -> Bool we can utilize. For the same week, we can set the toGranularity to .month:

let date1 = Date()
let date2 = Date()

print(Calendar.current.isDate(date1, equalTo: date2, toGranularity: .month))

Check If From This Year

Calendar has the method func isDate(_ date: Date, equalTo: Date, toGranularity: DateComponents) -> Bool we can utilize. For the same week, we can set the toGranularity to .year:

let date1 = Date()
let date2 = Date()

print(Calendar.current.isDate(date1, equalTo: date2, toGranularity: .year))

Before, After Or Equal

You can simply use the == < and > operators to determine if a date is before, after or equal to another one.

Equal example:

let date1 = Date()
let date2 = date1

print(date1 == date2) // True

Date1 is before Date2 example:

let date1 = Date()
let date2 = Date().addingTimeInterval(500)

print(date1 < date2) // True

Date1 is after Date2 example:

let date1 = Date().addingTimeInterval(500)
let date2 = Date()

print(date1 > date2) // True

Change Year, Month or Day

You can use DateComponents to change the year, month or day of any Date object. Here’s an example where we change all three:

let date = Date()
var dateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: date)

dateComponents.day = 1
dateComponents.month = 2
dateComponents.year = 2000

let newDate = Calendar.current.date(from: dateComponents)
If you liked this post, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts