Everything about Dates in Swift

arteko
arteko
Nov 17, 2019 · 3 min read

Recently I got into a freelance job and the app I was creating needed a lot of work with dates.

I’ve done a lot of researches so I decided to put it all together so you can have everything you need here.

There are 3 major classes to deal with dates: Date, DateComponents, DateFormatter and Calendar.

The Date type

This class only represents a date. You can’t do many things with it as it only represents a single date.

Here is what the documentation says about it:

A specific point in time, independent of any calendar or time zone.

This sums up perfectly what the Date type is.

This type is the base for every other type I mentioned before. Here is how to get the current date:

let now = Date()print(now) // 2019-11-17 09:51:05 +0000

By default, it uses the GMT+0 timezone.

This type also supports comparisons with the operators (<, ==) and with the compare method.

Dates can also be used to create a TimeInterval mainly with the method: timeIntervalSince(_:).

A TimeInterval is a typealias for the Double type as it represents a number of seconds separating two dates.

To have more control over the date, we can use DateFormatter.

DateFormatter

This one is used to create a Date from a String based on a given format.

It can also do the opposite which is converting a Date into a String based on a given format.

Here is an example:

You can also set the Locale ( for example US-us for American date or FR-fr for French) and the TimeZone of the DateFormatter.

If you want to see all the available formats for a DateFormatter you can go to nsdateformatter.com.

DateComponents

This class represents a date based on its components which means you can work with the parts of the dates you need.

For example, if you only need to manipulate the hour or the minutes from a date, you can use DateComponents.

Here is an example:

The components of a date are represented as optional integers which means it supports additions, substractions…

Calendars

This class is the class that manipulates all the others.

Using Calendar you can work with dates based on a calendar (remember the definition of Date in the documentation said that it was independent of a calendar).

The first consequence of that is you can create Date objects based DateCompenents or you can also do the opposite and much more things:

The second consequence of using dates with calendars is that you can do operations on dates such as adding 2 days to a Date or get the difference in hours between two dates:

Conclusion

There is much more to say about dates in Swift but what I covered here are the basics that you will need during your whole career as an iOS developer.

You don’t need to memorize everything that’s in this article. This is meant to be the article you check whenever you need some piece of information about the basics of dates.

Like every other programming concept, it’s important to have the basics about it but it’s even more important to know how to find the piece of information you need whenever you need it.

More From Medium

More from arteko

Related reads

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade