views:

971

answers:

4

Hi Everyone,

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.

A: 

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.

Ed Marty
Thanks Ed for your reply. The problem is that the years may differ. Of course, I could search for 4 digits and remove them. Before implementing this hack, are you sure there isn't an easier solution?
nicoko
Read the response by nicoko, it refers to a method of removing the year from the locale specific formatting string, not the output formatted date string.
Kendall Helmstetter Gelner
A: 

This function looks like it will get you most of the way there if not all...

+ (NSString *)dateFormatFromTemplate:(NSString *)templateoptions:(NSUInteger)optslocale:(NSLocale *)locale

http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDateFormatter/dateFormatFromTemplate:options: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

Eld
Unfortunately, that's specific to the desktop, and is not available for the iPhone version of the NSDateFormatter.
Ed Marty
Too bad, this solution would have been perfect! Thanks Eld for your answer.
nicoko
Sorry, didn't notice that this function wasn't for the iPhone =(
Eld
+1  A: 

Check out this question..... I had have the same issue before but have solved it by using the dateFormat not the dateStyle

Madhup
Thank you Madhup. I already answered my own question and posted the link to that answer.
nicoko