Converting Dates To Formatted Strings Easily in Swift

August 30th, 2024

⏱ Reading Time: 2 mins

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:

The easiest and fastest way to get the date as a string is by using the formatted() instance method as seen next:

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:

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:

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?

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:

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:

Also, here are all available options together in one place for easy reference:

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!

Stay Up To Date

Subscribe to my newsletter and get notifiied instantly when I post something new on SerialCoder.dev.

    We respect your privacy. Unsubscribe at any time.