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 and the corresponding timezones are set. 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?
CodePudding user response:
You can lock the date format in 24 hrs by declaring
formatter.dateFormat = "HH:mm"
CodePudding user response:
You can try out by using this code to get 24 hour time formate
func changeTimeFormate() {
let dateString = "11:00 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // fixes nil if device time in 24 hour format
let date = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "HH:mm"
let date24 = dateFormatter.string(from: date ?? Date())
print(date24)
}