NSDateFormatter ignores user's preferred date format

NSString* fmt = [NSDateFormatter dateFormatFromTemplate: @"yyyy-MM-dd HH:mm:ss"
                options: 0 locale: [NSLocale currentLocale]];

My understanding is that this should rewrite the format string to something that reflects the users current settings. When I run it, it does seem to reflect the "24-Hour Time" setting under General -> Date & Time. But it doesn't reflect the "Date Format" setting under General -> Language & Region. It seems that the date format is always the default for the region set in General -> Language & Region -> Region. It also seems to insert an unexpected comma.

Specifically:

Region=US, fmt="MM/dd/yyyy, h:mm:ss a"
Region=UK, fmt="dd/MM/yyyy, h:mm:ss a"

irrespective of the "Date Format" setting. What's going on?

It seems to be doing what the documentation says it does:

https://developer.apple.com/documentation/foundation/nsdateformatter/1408112-dateformatfromtemplate/

See under "Return Value" and "Discussion" for an example of this behavior.

Note that this code:

NSDateFormatter* f = [[NSDateFormatter alloc] init];
f.locale = [NSLocale currentLocale];
f.dateStyle = NSDateFormatterShortStyle;
f.timeStyle = NSDateFormatterShortStyle;
NSString* fmt = f.dateFormat;

Does return a format string that reflects General -> Language & Region -> Date Format:

Region=UK, Date Format=2023-08-19, fmt="y-MM-dd, h:mm a"
Region=UK, Date Format=8/19/23, fmt="dd/MM/yyyy, h:mm:ss a"

See under "Return Value" and "Discussion" for an example of this behavior.

Could you be specific about what you see in that documentation? As I read it, it says that the components in the returned format string should be arranged according to the locale. It does not relate that to the various Settings app settings.

It seems to me that:

  1. The Settings app language, region and 24hr vs. am/pm settings are reflected in public properties of NSLocale.

  2. The Settings app date format setting is reflected in a private property of NSLocale, which I can see in the debugger.

  3. Setting NSDateFormatter’s locale, dateStyle and timeStyle and reading dateFormat returns a template based on (1) and (2).

  4. dateFormatFromTemplate returns a template based only on (1). This seems wrong to me.

FB13345817

NSDateFormatter ignores user's preferred date format
 
 
Q