Iphone 到NSDate的字符串

Iphone 到NSDate的字符串,iphone,objective-c,nsdate,Iphone,Objective C,Nsdate,我正在尝试将字符串转换为日期。我们使用了以下代码。但是约会搞错了 谢谢 NSString *dateString = [NSString stringWithFormat:@"12-07-2011"]; //NSString *dateString = [NSString stringWithFormat:@"%d-%d-%d", selectedDate.day, selectedDate.month, selectedDate.year]; NSLog(@"%@",dateString);

我正在尝试将字符串转换为日期。我们使用了以下代码。但是约会搞错了

谢谢

NSString *dateString = [NSString stringWithFormat:@"12-07-2011"];
//NSString *dateString = [NSString stringWithFormat:@"%d-%d-%d", selectedDate.day, selectedDate.month, selectedDate.year];
NSLog(@"%@",dateString);

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"dd-mm-yyyy"];

NSDate* d = [dateFormatter dateFromString:dateString];
NSLog(@"%@",d);               
                      
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selected Date" message: [NSString stringWithFormat:@"%@",d] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
使用:


问题是你用的是分钟而不是月份<代码>M

[dateFormatter setDateFormat:@“dd-MM-yyyy”]


您需要进一步解释。你有什么约会?
[dateFormatter setDateFormat:@"dd-MM-yyyy"];