I am currently facing a problem with DateIntevalFormatter
. When using the formatter with dateStyle = .long
and timeStyle = .none
the string output for some locales is not in the expected formatting. While the output for locales like en_US, de_de or fr_fr is as expected, locales like zh_Hans, ja and ko is not consistent with the dateStyle = .long
of a DateFormatter
. Using the dateTemplate
/setLocalizedDateFormatFromTemplate()
with template MMMMddyyyy
seems to provide a consistent output.
Is that maybe a bug in DateIntervalFormatter
?
Here some exemplary output of the below Playground code: It seems that the forum is not accepting some of the Chinese, Korean or Japanese characters, therefore you can only find the actual output of below code as attachment.
import UIKit
func printDate(starteDate: Date, endDate: Date, locale: Locale) {
let interval = DateIntervalFormatter()
interval.locale = locale
interval.dateTemplate = "MMMMddyyyy"
let formatter = DateFormatter()
formatter.locale = locale
formatter.setLocalizedDateFormatFromTemplate("MMMMddyyyy")
print("Locale: \(locale), template: MMMMddyyyy")
print("Interval = \(interval.string(from: starteDate, to: endDate))")
print("Format = \(formatter.string(from: starteDate))")
print("Locale: \(locale), dateStyle: long")
interval.dateStyle = .long
interval.timeStyle = .none
formatter.dateStyle = .long
formatter.timeStyle = .none
print("Interval = \(interval.string(from: starteDate, to: endDate))")
print("Format = \(formatter.string(from: starteDate))")
}
let date1 = DateComponents(calendar: Calendar(identifier: Calendar.Identifier.gregorian), timeZone: TimeZone(abbreviation: "UTC"), year: 2019, month: 12, day: 20).date
let date2 = DateComponents(calendar: Calendar(identifier: Calendar.Identifier.gregorian), timeZone: TimeZone(abbreviation: "UTC"), year: 2019, month: 12, day: 22).date
printDate(starteDate: date1!, endDate: date2!, locale: Locale(identifier: "en_US"))
printDate(starteDate: date1!, endDate: date2!, locale: Locale(identifier: "de_de"))
printDate(starteDate: date1!, endDate: date2!, locale: Locale(identifier: "zh-Hans"))
printDate(starteDate: date1!, endDate: date2!, locale: Locale(identifier: "ko"))
printDate(starteDate: date1!, endDate: date2!, locale: Locale(identifier: "ja"))