NSDateFormatter: Date according to currentLocale, without Year
I think you need to take a look at:
+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale
As per the docs:
Returns a localized date format string representing the given date format components arranged appropriately for the specified locale. Return Value A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.
The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.
Discussion
Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale).
The following example shows the difference between the date formats for British and American English:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSString *dateFormat;
// NOTE!!! I removed the 'y' from the example
NSString *dateComponents = @"MMMMd"; //@"yMMMMd";
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
[usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
[gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);
// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y
Extra code (add this to the code above):
//
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.locale = gbLocale;
formatter.dateFormat = dateFormat;
NSLog(@"date: %@", [formatter stringFromDate: [NSDate date]]);
See here: NSDateFormatter Class Reference
The two examples you give are very different from each other. One uses the abbreviated month name while the other uses the 2-digit month number. One uses a day ordinal ("2nd") while the other simply uses the 2-digit day number.
If you can accept using the same general format for all locales then make use of NSDateFormatter dateFormatFromTemplate:options:locale:
.
NSString *localFormat = [NSDateFormatter dateFormatFromTemplate:@"MMM dd" options:0 locale:[NSLocale currentLocale]];
The result of this call will give back a format string you can use with NSDateFormatter setDateFormat:
. The order of the month and day will be appropriate for the locale as well as any additional punctuation that should be added.
But again, this won't solve your exact needs because of the completely different formats you appear to want for each locale.