Say I have a Text.DateStyle.timer in WidgetKit and I want to set it so that it displays hours:minutes only, instead of hours:minutes:seconds. How can I go about this?
Asked
Active
Viewed 233 times
2 Answers
0
To display hours and minutes only in WidgetKit, you can use the DateFormatter class in SwiftUI.
Here is an example in SwiftUI:
import SwiftUI
import WidgetKit
struct MyWidgetEntryView : View {
static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "h:mm"
return formatter
}()
var body: some View {
VStack {
Text(Self.dateFormatter.string(from: Date()))
.font(.largeTitle)
Spacer()
}
}
}
struct MyWidgetEntryView_Previews: PreviewProvider {
static var previews: some View {
MyWidgetEntryView()
}
}

baohoang
- 616
- 1
- 5
- 13
-
Thanks for your reply, however I don't believe this solution is compatible with Date.timer (not to be confused with Date.time). – David Mar 05 '23 at 05:01
-1
In iOS 15.0+ you can use the formatted function:
Date().formatted(date: .omitted, time: .shortened)

Jonas Lang
- 213
- 1
- 2
- 12
-
Thanks for your reply, however I don't believe this solution is compatible with Date.timer (not to be confused with Date.time). – David Mar 05 '23 at 05:02