fs_tigre
fs_tigre

Reputation: 10738

Text DateStyle with short month name in SwiftUI

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?

Upvotes: 0

Views: 1426

Answers (2)

Asperi
Asperi

Reputation: 257653

SwiftUI 3+

We have now explicit initializer for that, like

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

Upvotes: 4

Steven-Carrot
Steven-Carrot

Reputation: 3051

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))

formatted(date:time:) (Apple Developer Documentation)

  • Generates a locale-aware string representation of a date using specified date and time format styles.

Upvotes: 2

Related Questions