Introduction
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// // DateTimeUtils.swift // // Copyright © 2017 Borombo. All rights reserved. // import Foundation class DateTimeUtils { // Unique instance (Singleton) static let instance = DateTimeUtils() } |
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.
1 2 3 |
// Select the locale static let LOCALE = "FR-fr" static let SAVED_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ" |
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.
1 2 3 4 5 |
// Different formatter private let french = DateFormatter() private let dateOnly = DateFormatter() private let hourOnly = DateFormatter() private let savedFormat = DateFormatter() |
Initialization
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
init() { french.dateStyle = .long french.timeStyle = .short dateOnly.dateStyle = .long dateOnly.timeStyle = .none hourOnly.dateStyle = .none hourOnly.timeStyle = .short french.locale = Locale(identifier: DateTimeUtils.LOCALE) dateOnly.locale = Locale(identifier: DateTimeUtils.LOCALE) hourOnly.locale = Locale(identifier: DateTimeUtils.LOCALE) savedFormat.dateFormat = DateTimeUtils.SAVED_FORMAT } |
Functions
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
1 2 3 |
func getDateFromSaved(saved: String)-> Date{ return savedFormat.date(from: saved)! } |
Obviously I also need the reverse action
1 2 3 4 |
// Transform a Date object to the saved format func dateToSavedFormat(date: Date) -> String { return savedFormat.string(from: date) } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Transform a Date object to a Full Date-Time String func dateToFrench(date: Date) -> String{ var stringDate = french.string(from: date) stringDate = stringDate.capitalized return stringDate.replacingOccurrences(of: "À", with: "à") } // Get the String full date-time from the string saved format func savedToFrench(savedDate: String) -> String { return french.string(from: (savedFormat.date(from: savedDate))!) } // Get only the date from the string saved format func getDateFromSaved(savedDate: String) -> String { return dateOnly.string(from: (savedFormat.date(from: savedDate))!) } // Get only the hour from the string saved format func getHourFromSaved(savedDate: String) -> String { return hourOnly.string(from: (savedFormat.date(from: savedDate))!) } |
Utilization
Here is one of the use that I do with
1 2 3 4 5 |
playerData["birthDate"] = DateTimeUtils.instance.dateToSavedFormat(date: selectedDate) // OR birthDateLabel.text = DateTimeUtils.instance.getDateFromSaved(savedDate: (player?.birthDate)!) |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
// // DateTimeUtils.swift // // Created by Erwan Rombo on 19/10/2017. // Copyright © 2017 Borombo. All rights reserved. // import Foundation class DateTimeUtils { // Unique instance (Singleton) static let instance = DateTimeUtils() // Select the locale static let LOCALE = "FR-fr" static let SAVED_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ" // Different formatter private let french = DateFormatter() private let dateOnly = DateFormatter() private let hourOnly = DateFormatter() private let savedFormat = DateFormatter() init() { french.dateStyle = .long french.timeStyle = .short dateOnly.dateStyle = .long dateOnly.timeStyle = .none hourOnly.dateStyle = .none hourOnly.timeStyle = .short french.locale = Locale(identifier: DateTimeUtils.LOCALE) dateOnly.locale = Locale(identifier: DateTimeUtils.LOCALE) hourOnly.locale = Locale(identifier: DateTimeUtils.LOCALE) savedFormat.dateFormat = DateTimeUtils.SAVED_FORMAT } func getDateFromSaved(saved: String)-> Date{ return savedFormat.date(from: saved)! } // Transform a Date object to a Full Date-Time String func dateToFrench(date: Date) -> String{ var stringDate = french.string(from: date) stringDate = stringDate.capitalized let res = stringDate.replacingOccurrences(of: "À", with: "à") return res } // Transform a Date object to the saved format func dateToSavedFormat(date: Date) -> String { return savedFormat.string(from: date) } // Get the String full date-time from the string saved format func savedToFrench(savedDate: String) -> String { return french.string(from: (savedFormat.date(from: savedDate))!) } // Get only the date from the string saved format func getDateFromSaved(savedDate: String) -> String { return dateOnly.string(from: (savedFormat.date(from: savedDate))!) } // Get only the hour from the string saved format func getHourFromSaved(savedDate: String) -> String { return hourOnly.string(from: (savedFormat.date(from: savedDate))!) } } |
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