Formatting Date Interval in Swift

Mykhailo Moiseienko
2 min readMay 24, 2023

--

Dates and time intervals are fundamental components of many applications, ranging from event scheduling to data analysis.

In this article, we will explore the capabilities of DateIntervalFormatter and discover how it can help us create user-readable strings of the form <start> - <end>.

To start, you need to create an instance of DateIntervalFormatter. Once you have the formatter instance, you can customize its behavior by setting the dateStyle and timeStyle properties.

let formatter = DateIntervalFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short

The dateStyle property allows you to specify the style of the date component in the formatted interval, such as short, medium, long, or full. On the other hand, the timeStyle property determines the style of the time component, including options like none, short, medium, long, or full.

By combining different dateStyle and timeStyle values, you can achieve various formatting combinations to suit your specific needs. These properties provide flexibility in presenting date intervals in a way that best communicates the desired information to your users.

Examples

  • dateStyle = .short and timeStyle = .none

“5/24/23–5/26/23”

  • dateStyle = .long and timeStyle = .none

“May 24–26, 2023”

  • dateStyle = .full and timeStyle = .none

“Wednesday, May 24 — Friday, May 26, 2023”

  • dateStyle = .short and timeStyle = .short

“5/24/23, 12:15 PM — 5/26/23, 2:45 PM”

There are 2 ways to obtain a string representation of a date interval using DateIntervalFormatter.

  • Use two instances of the Date class, which represent the start and end dates of the interval.
let fromDate = Date()
let toDate = Date(timeIntervalSinceNow: 9_000) // 2h 30m

let formattedInterval = formatter.string(from: fromDate, to: toDate)
print(formattedInterval) // Output: "5/24/23, 11:00 AM – 1:30 PM"
  • Use an instance of the DateInterval class, which represents a specific time interval. This way allows you to directly obtain the formatted string representation of the interval without needing to specify individual start and end dates.
let interval = DateInterval(start: .now, duration: 9_000)

let formattedInterval = formatter.string(from: interval)
print(formattedInterval) // Output: "5/24/23, 11:00 AM – 1:30 PM"

By default, the DateIntervalFormatter uses the system calendar, locale, and timezone to format date intervals. However, it’s important to note that these parameters can be easily customized to suit specific requirements.

With DateIntervalFormatter, you have the flexibility to set a different calendar, allowing you to work with alternative calendar systems or regional variations.

Adjusting the timezone parameter enables you to display the date intervals in a specific time zone, useful for international applications or when dealing with users in different regions.

formatter.timeZone = TimeZone(abbreviation: "PST")

Additionally, you can specify a different locale to ensure that the formatting adheres to the language and cultural conventions of your target audience.

formatter.locale = Locale(identifier: "en_US")

If you’re interested in learning how to format durations using DateComponentsFormatter — be sure to check out this article.

Thanks for reading!

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mykhailo Moiseienko
Mykhailo Moiseienko

Written by Mykhailo Moiseienko

📱 Senior iOS Software Engineer. ✍️ Writing about iOS Development, Swift, and SwiftUI.

No responses yet

Write a response