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"))
}
Post
Replies
Boosts
Views
Activity
I'm trying to display an Int in Hebrew. so for example 123 should display אבג
1 = א
2 = ב
3 = ג
I have tried specifying the locale based on a the answer to a different post of mine where the solution was to specify the numbering system in the locale Locale(identifier: "he-IL@numbers=hebr")
print(123.formatted(.number.locale(Locale(identifier: "en@numbers=hebr"))))
// Output: 123
When setting the same locale for dates it formats properly with Hebrew numbers.
However if I do Arabic instead of Hebrew the numbers display properly in arabic the result for this is ``
print(123.formatted(.number.locale(Locale(identifier: "en@numbers=Arab"))))
// Output: ١٢٣
Below is the code I've tried running in a playground:
This is the code I ran in the playground
I'm trying to format a date to show the current time with the least amount of digits as possible. Using Date.FormatStyle and adding .hour() is the closest I can get. See the code below ↓
var formatStyle = Date.FormatStyle()
var hourMinute: Date.FormatStyle{
formatStyle
.hour()
.minute()
}
// Output 3:15 PM
var hourMinuteDefaultDigits: Date.FormatStyle{
formatStyle
.hour(.defaultDigits(amPM: .omitted))
.minute()
}
// Output 03:15
My goal is to output 3:15 without the amPm but that doesn't seem possible using Date.FormatStyle.
Based on the documentation this is the function signature
func hour(_ format: Date.FormatStyle.Symbol.Hour = .defaultDigits(amPM: .abbreviated)) -> Date.FormatStyle
// Output 3:15 PM
For some reason if I put .omitted for the amPM then defaultDigits and twoDigits return the same 2 digits for the hour