[ad_1]
Right here is my code 👇
In my utility, when the person creates an occasion, the occasion time is proven to the person. Nonetheless, if the telephone is in a 12 time zone and is in a language aside from English, this time returns null.
func utcToLocal(dateStr: String) -> String? {
let isCurrentDevice12HoursFormat = DateFormatter.dateFormat(fromTemplate: "j", choices: 0, locale: Locale.present)?.vary(of: "a") != nil
let isDateStr12HoursFormat = dateStr.final == "M"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: isDateStr12HoursFormat ? "en_US_POSIX" : "en_UK_POSIX")
dateFormatter.dateFormat = isDateStr12HoursFormat ? "yyyy-MM-dd' 'h:mm:ss a" : "yyyy-MM-dd' 'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
if let date = dateFormatter.date(from: dateStr) {
dateFormatter.timeZone = TimeZone.present
dateFormatter.dateFormat = isCurrentDevice12HoursFormat ? "yyyy-MM-dd' 'h:mm:ss a" : "yyyy-MM-dd' 'HH:mm:ss"
return dateFormatter.string(from: date)
}
return nil
}
[ad_2]
