Objective C / iPhone: How do I extract the actual

2019-07-21 17:44发布

问题:

According to this site: http://iosdevelopertips.com/cocoa/date-formatter-examples.html

there is a class that handles formatting, which takes in a set of constants/enums (e.g. NSDateFormatterShortStyle) to the "setDateStyle" property/method.

Somehow the NSDateFormatter knows to retrieve the proper locale-specific date format. What I want is to be able to retrieve the default formats based on the user's choice of region format. I have a feeling it is stored in NSLocale, but that does not seem to expose anything that will retrieve the format strings.

Is there a way to extract the formats? It has to be in memory somewhere; I'm hoping the retrieval mechanism is exposed somewhere.

I've looked in several places, but the only answers I get are a lesson on how to create an NSDate from a custom format.

回答1:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];
NSString *dateFormat = [df dateFormat];
NSLog(@"Date format: %@", dateFormat);
[df release];

Just tested on OS X but this should also work in iOS.