Is there a way using Date.FormatStyle
to make the numbers also show in Hebrew the same way DateFormatter
does?
For some reason using Date.FormatStyle
doesn't seem to have the ability to show the numbers locally.
Here's some sample code, you can see that using DateFormatter
the locale is respected and it displays properly but using Date.FormatStyle
the numbers are still regular numbers.
Update: Here's a modified version of DateFormatter. The goal is using Date.FormatStyle and getting the same result as DateFormatter
import SwiftUI
struct ContentView: View {
let date = Date.now
var body: some View {
List{
Text(date.formatted(formatStyle.attributed))
.environment(\.locale,
Locale(languageCode: .hebrew, script: .hebrew, languageRegion: .israel))
.environment(\.calendar, Calendar(identifier: .hebrew))
Text(date, format: formattttttt)
.environment(\.locale,
Locale(languageCode: .hebrew, script: .hebrew, languageRegion: .israel))
.environment(\.calendar, Calendar(identifier: .hebrew))
Text(dateFormatted(date: date))
}
}
func dateFormatted(date: Date) -> String{
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .hebrew)
dateFormatter.locale = Locale(identifier: "he")
dateFormatter.dateStyle = .full
return dateFormatter.string(from: date)
}
let formatStyle = Date.FormatStyle(
date: .complete,
time: .omitted,
locale: Locale(languageCode: .hebrew, script: .hebrew, languageRegion: .israel),
calendar: Calendar(identifier: .hebrew)
)
let formattttttt = Date.FormatStyle(
date: .complete, time: .omitted, locale: Locale(identifier: "he"), calendar: Calendar(identifier: .hebrew)).locale(Locale(identifier: "he"))
}