iphone shortdate with 4 digit year
Just in case that someone stumbles in here as I did 'cos I needed such a thing with a 2 digit year in a localized date format and this is a really old question, here is my solution:
NSLocale* curLocale = [NSLocale currentLocale];
NSString* dateFmt = [NSDateFormatter dateFormatFromTemplate: @"ddMMyyyy"
options: 0
locale: curLocale];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:dateFmt];
NSString* retText = [formatter stringFromDate:refDate];
return retText;
Maybe someone could use it sometime...
This is an old thread, but because I was trying to do what Dave wanted and couldn't and because none of the given answers were correct, here is the solution (in case anyone ever want's the same thing):
NSString *FormatString = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy HH:mm" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:FormatString];
//Parse date here with the formater (like [formatter stringFromDate:date])
[formatter release];
If you need four digit years, set the dateformat:
using the exact format you'd like. The downside is you lose automatic locale formatting.
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
...
birthday.text = [dateFormatter stringFromDate:p.Birth];
If you need localization then you could try modifying the date format of the short style.
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
if ([[dateFormatter dateFormat] rangeOfString:@"yyyy"].location == NSNotFound) {
NSString *fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString:@"yy" withString:@"yyyy"];
[dateFormatter setDateFormat:fourDigitYearFormat];
}
Updated with Bugfix from Max Macleod