Manage Date object [Swift] - Image

How to manage Date Object without third libraries [Swift] 📆


Recently I worked on an iOS app and I had to handle some Date object in my project. I made some research about it and I found a lot of third libraries which handle the management of Date/Time object. With them, we are able to handle some picker, format them to display it to the user, and play with the data and the format.

I didn’t know which one to use and I didn’t really wanted to use one. Handle Date object is not too complicated to do by yourself so I preferred doing it better than adding another third libraries. In general I try to use less third libraries as possible for performance, dependencies and app size purpose.

So let’s dive in !

One file : DateTimeUtils.swift

I will use only one file that will be accessible everywhere within my app. Usually I use a folder Utils for that type of file. So let’s create that file like this :

After that, I will set the Locale and the saved format. The application that I made was made only for French people so I only used the French locale, but you can set the one that you want . The saved format is the format that I will save the date for my database.

Then, I will set different formatter. I will use them for… formate my Date object. Actually I need four of them, one for display it in the app, two for only take the date or the hour, and the last one for the saved format on database.  Obviously, you do it for the format that you need.


I will need an init function to initialize the format of all my formatters. Depending of your needs, you setup your formatter like you want.


The final part is to create all the functions that I will need within my app :

The first one for example is to get the Date object from the formatted String saved in my database

Obviously I also need the reverse action

I needed few others functions for my app, but they all works in the same way. You can make the one that you will need in your app.


Here is one of the use that I  do with

As all your variable are declared at the beginning, you can easily change the format or the local of your app without to have to write all the functions again.

Here it’s how looks the final file :

Tell me if you use other customs ways to manage your Date object in swift in comments, it’s always good to have multiple ways to do it !

Add a Comment

Your email address will not be published. Required fields are marked *