Sunday, May 17, 2026
HomeiOS DevelopmentiOS Swift app - 24 hour format from Settings app isn't revered...

iOS Swift app – 24 hour format from Settings app isn’t revered for Germany and France

[ad_1]

I’ve to show date and time in an iOS app primarily based on locale (Area) and time format (24-Hour vs 12-Hour) set within the machine Settings app.

I’m utilizing under code for a similar :-

class func getDateTimeString(_ fromDate: Date?) -> String {
    var dateTimeString = ""
    if let date = fromDate {
        let format = DateFormatter()
        format.timeZone = .present
        format.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMM dd, YYYY hh:mm a", choices: 0, locale: Locale.present)
        dateTimeString = format.string(from: date)
    }
    return dateTimeString
}

This works good to show date as per any locale (Area) set in Settings app. Like for India, it shows date as dd-mmm-yyyy. For US – mmm dd, yyyy. For Germany – dd. mmm yyyy

Nevertheless, as per time format this works good for areas like US, UK, India, Australia however not for France and Germany. As in it shows the time in 12-hour (AM/PM) format or 24-hour format primarily based on what is chosen within the Settings app for US, UK, India and Australia however for Germany and France it shows time solely in 12-hour format with AM and PM, irrespective of what’s set in time format in Settings app.

Is that this anticipated? If sure, why? If no, what am I lacking right here? I noticed one factor although. Altering the Area to US, UK, India, Australia in Settings modifications the time format robotically to 12 hour format below Date&Time settings. Nevertheless, it stays 24-hour format when altering area to Germany or France.

To resolve the 12-hour format subject for areas like Germany and France, I took cues from https://stackoverflow.com/a/49438640/3148811 and needed to modify above code to –

class func getDateTimeString(_ fromDate: Date?) -> String {
    var dateTimeString = ""
    if let date = fromDate {
        let format = DateFormatter()
        format.timeZone = .present
        if let dateFormatterString = DateFormatter.dateFormat(fromTemplate: "j", choices: 0, locale: Locale.present), dateFormatterString.firstIndex(of: "a") != nil {
            format.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMM dd, YYYY hh:mm a", choices: 0, locale: Locale.present)
        } else {
            format.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMM dd, YYYY HH:mm", choices: 0, locale: Locale.present)
        }
        dateTimeString = format.string(from: date)
    }
    return dateTimeString
}

Is that this a great way to resolve the identical?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments