Getting dates as strings in Swift is not a rare task. The format of the presented date depends on the requirements of the app, whether date should be blended with other text or not, significancy in the context and other factors. Thankfully, there is an instance method in the Date struct that makes it really easy to present a date as a string, providing quite a few options both for the date and for the time part.
Presenting formatted dates
Let’s start by assigning the current date to a constant:
1 2 3 |
let date = Date() |
The easiest and fastest way to get the date
as a string is by using the formatted()
instance method as seen next:
1 2 3 |
date.formatted() |
The output will be this:
29/8/2024, 11:32 AM
See that the date is presented in a numerical format, while the time is shortened showing only the hour, minutes and time period. No seconds or timezone are present.
Note: The output date string will be automatically localized using the user’s device locale settings.
We can override the above outcome of the formatted date by providing specific values for both the date and time parts of the result. To do that, we have to use the formatted(date:time:)
method overload, where we’ll supply the desired format for both parts as arguments.
Let’s start with the following:
1 2 3 |
date.formatted(date: .complete, time: .omitted) |
This line will display the current date as a long string that includes the current day, date, month and year, but it will not show the time part at all. The .omitted
value is an option for both parameters, allowing us to hide the one or the other when we don’t need them visible. Here’s what the above prints:
Thursday, 29 August 2024
Here are the rest date formatting values we can use as arguments:
1 2 3 4 5 6 7 8 9 10 |
date.formatted(date: .long, time: .omitted) // Output: 29 August 2024 date.formatted(date: .numeric, time: .omitted) // Output: 29/8/2024 date.formatted(date: .abbreviated, time: .omitted) // Output: Aug 29, 2024 |
Have you wondered what is going to happen if we omit both the date and the time part? What is going to be displayed if we specify this?
1 2 3 |
date.formatted(date: .omitted, time: .omitted) |
Well, despite any meaningless efforts to produce nothing, the date and time will be shown normally, just like we used the formatted()
method with no arguments at all.
Regarding the time part, we have the following built-in options at our disposal when we don’t need to omit it:
1 2 3 4 5 6 7 8 9 10 |
date.formatted(date: .omitted, time: .complete) // Output: 11:32:08 AM GMT+3 date.formatted(date: .omitted, time: .shortened) // Output: 11:32 AM date.formatted(date: .omitted, time: .standard) // Output: 11:32:08 AM |
Summary
We can use either the default styling, or combine any of the built-in values in order to get a date as a string the way we need it. For example, the following will display the date part in a complete format, while the time is shortened:
1 2 3 4 |
date.formatted(date: .complete, time: .shortened) // Output: Thursday, 29 August 2024 at 11:32 AM |
Also, here are all available options together in one place for easy reference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Date part: .complete .long .numeric .abbreviated .omitted // Time part: .complete .shortened .standard .omitted |
Besides all these system-provided formatting styles, it’s also possible to define custom ones. However, we’ll discuss about that in another tutorial. What you’ve read here is good enough in the majority of use cases. So, go ahead and use formatted date strings in SwiftUI text views, in UIKit, AppKit, or anywhere else you need them.
Thank you for reading!