views:

298

answers:

2

Hi.

I need the date as a string but not the time and it has to be localized.

So for example USA should be Sep 25 2009 but for New Zealand it would be 25 Sep 2009. I can get the date into a string by specifying the format "MMM dd YYYY" but It's not localized.

Any ideas?

+3  A: 
[NSDateFormatter localizedStringFromDate:[NSDate date]
                               dateStyle:NSDateFormatterMediumStyle
                               timeStyle:0]
RJ
is this available on the iPhone? doesn't appear to be in the iPhone-specific NSDateFormatter docs.
David Maymudes
timeStyle should be set to kCFDateFormatterNoStyle. It happens to be 0 but you really shouldn't hardcode constants when there is a named constant provided.
progrmr
+1  A: 

The key is to send setTimeStyle:kCFDateFormatterNoStyle to the dateFormatter. That sets the dateFormatter so that it will only format the date and not output the time:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
[dateFormatter setLocale: [NSLocale currentLocale]];    
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // or whichever style...
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle];   
NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];

This gives me the following output:

12/18/09
progrmr