I am using DateFormatter with a custom calendar in Text(_:formatter:)
, but the text does not display the correct date. But if using DateFormatter.string(from:)
, the correct date will be displayed.
So, what is the difference?
The example:
extension DateFormatter {
static func hijri(_ format: String) -> DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.calendar = .init(identifier: .islamicUmmAlQura)
return formatter
}
}
struct TextFormatterView: View {
var body: some View {
VStack {
Text(DateFormatter.hijri("dd, MMMM yyyy").string(from: Date()))
// 24, Rabiʻ I 1445 👍
Text(Date(), formatter: DateFormatter.hijri("dd, MMMM yyyy"))
// 09, October 2023 ❌
}
}
}