The solution is to use the getObjectValue: forString: range: error: method of NSDateFormatter and set the correct date and time style declaration:
- (NSDate *)dateWithString:(NSString *)string locale:(NSString *)localeIdentifier {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
[formatter setLocale:locale];
[locale release];
NSDate *date = nil;
NSRange range = NSMakeRange(0, [string length]);
NSError *error = nil;
BOOL converted = NO;
converted = [formatter getObjectValue:&date forString:string range:&range error:&error];
[formatter release];
return converted? date : nil;
}
Example:
NSString *italianDate = @"30/10/2010";
NSString *italianLocale = @"it_IT";
NSDate *date = [myCustomFormatter dateWithString:italianDate locale:italianLocale];
source
share