I have 24-Hour Time switch ON on the device and testing the iOS App for different timezones which gives me the correct time format.
Ex:
- 23:00 PDT if 24-Hour Time switch ON
- 11 PM PDT if 24-Hour Time switch OFF
But, if I change my region in the device to Japan or France for any timezone. I observe the time format is ignoring the 24-Hour Time switch and always showing the 12-hour format. Whereas, if the region is set to the United States or India I see correct results.
func formattedCloseTime(for date: DateTimeZone) -> String {
let formatter = dateProvider.formatter
formatter.timeZone = date.timeZone
formatter.locale = .autoupdatingCurrent
let stopTimeMinutes = dateProvider.minutesOfDay(from: date.value, timeZone: date.timeZone)
// formatter.dateFormat = stopTimeMinutes == 0
// ? Constants.DateFormat.hourFormat
// : Constants.DateFormat.hourMinuteFormat
formatter.setLocalizedDateFormatFromTemplate(stopTimeMinutes == 0
? Constants.DateFormat.hourFormat
: Constants.DateFormat.hourMinuteFormat)
return formatter.string(from: date.value)
}
I'm surprised why is it happening only in a few regions. Is there any relation between region and time format? How do I solve this case?
NOTE: I don't want to firm the time format to 24 hours only. I want it to take the user's device settings.
Solution Apple API of setLocalizedDateFormatFromTemplate doesn't look like working correctly for all regions. So, I considered taking the user's settings of the 24-hour switch value and displaying the format accordingly. You can check out this to get the user's settings 24-hour value.
var isTwentyFourHourTimeSet: Bool {
let formatString = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current)!
return !formatString.contains("a")
}