Displaying dates in SwiftUI has been made so easy now, that there is almost no reason to use DateFormatter anymore to display dates in SwiftUI. We will go over several examples to show how powerful this API has become, and there are more configurations that we aren’t going to show here. Hopefully, this article encourages you to check out everything you can do with dates and times in your app.
Before we get lost in all the cool things we are about to learn, please take a couple seconds to follow me and clap for the article so that we can help more people learn about this useful content.
Keep in mind, that these are formatted for my current locale (in America), another great part about this API, is that these examples will be formatted for whatever locale you run them in! That’s right, we as developers no longer have to worry about a date format looking incorrect in another country, and we get that formatting for free!
// ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text(Date.now, format: .dateTime.day().month().year())
// Oct 7, 2023
Text(Date.now.formatted(date: .abbreviated, time: .omitted))
// Oct 7, 2023
Text(Date.now.formatted(date: .long, time: .omitted))
// October 7, 2023
Text(Date.now.formatted(date: .complete, time: .omitted))
// Saturday, October 7, 2023
Text(Date.now.formatted(date: .numeric, time: .omitted))
// 10/7/2023
Text(Date.now.formatted(date: .omitted, time: .complete))
// 6:53:39 PM CDT
Text(Date.now.formatted(date: .omitted, time: .standard))
// 6:53:39 PM
Text(Date.now.formatted(date: .omitted, time: .shortened))
// 6:53 PM
Text(Date.now.formatted(.dateTime.dayOfYear()))
// 280
Text(Date.now.formatted(.dateTime.era()))
// AD
Text(Date.now.formatted(.dateTime.quarter()))
// Q4
Text(Date.now.formatted(.dateTime.week()))
// 40
Text(Date.now.formatted(.dateTime.weekday()))
// Sat
Text(Date.now.formatted(.dateTime.year(.twoDigits)))
// 23
Text(Date.now.formatted(.dateTime.month(.narrow)))
// O (this is an O for October, not a zero)
Text(Date.now.formatted(.dateTime.hour(.twoDigits(amPM: .wide))))
// 06 PM
}
.padding()
}
}
#Preview {
ContentView()
}
If you are looking for more ways to format dates and times, check out another article I wrote that shows more methods for iOS 18 and Xcode 16 here: https://medium.com/@jpmtech/new-text-formatters-in-ios-18-4c9037e1d980
If you got value from this article, please consider following me, clapping for this article, or sharing it to help others more easily find it.
If you have any questions on the topic, or know of another way to accomplish the same task, feel free to respond to the post or share it with a friend and get their opinion on it.
If you want to learn more about native mobile development, you can check out the other articles I have written here: https://medium.com/@jpmtech
If you want to see apps that have been built with native mobile development, you can check out my apps here: https://jpmtech.io/apps
Thank you for taking the time to check out my work!