Reputation: 195
I would like to display only the day and month of a date. Basically the NSDateFormatterLongStyle but without the year.
The problem I'm facing is the localization. In the english version it has to show for example 'January 14' and in french '14 Janvier'.
Is there an easy way to print such dates? Or do I have use ifelse statements for checking the current locale and set the day before or after the month?
Thanks in advance for your help.
Upvotes: 4
Views: 10989
Reputation: 8114
Check out this question.....
I had have the same issue before but have solved it by using the dateFormat
not the dateStyle
Upvotes: 2
Reputation: 195
I found this answer: Using NSDateFormatter on the iPhone, protect locale but lose date components
It works great!
Upvotes: 1
Reputation: 984
This function looks like it will get you most of the way there if not all...
+ (NSString *)dateFormatFromTemplate:(NSString *)templateoptions:(NSUInteger)optslocale:(NSLocale *)locale
"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)."
I think if you use their example provided, just remove the y from
NSString *dateComponents = @"yMMMMd";
and it will give you a string without the year portion for the given locale
Upvotes: 1
Reputation: 39700
It seems like an if/else wouldn't be sufficient to the task of catching every case. I would use the NSDateFormatterLong style, then just find/replace to remove the year from the string.
Upvotes: -1