Home > Software design >  Text DateStyle with short month name in SwiftUI
Text DateStyle with short month name in SwiftUI

Time:07-12

The following outputs the complete month name, e.g. July 11, 2022

  Text("\(Date().now, style: .date)") // output: July 11, 2022

Is there a way to make it show just the short month name, e.g. Jul 11, 2022 or 06/11/22?

CodePudding user response:

Show current date without time in a short format.

 //Jul 11, 2022
 Text(Date().formatted(date: .abbreviated, time: .omitted))

 //7/11/2022
 Text(Date().formatted(date: .numeric, time: .omitted))

CodePudding user response:

SwiftUI 3

We have now explicit initializer for that, like

Text(Date.now, format: Date.FormatStyle(date: .abbreviated, time: .omitted))
  • Related