2011-07-12 19 views
2

現在の文字列に変換しようとしています。次のコードを使用しています。しかし、間違った日付を取得します。NSDateへの文字列

おかげで、

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]; 
+1

。あなたは何時までですか? – gcamp

答えて

6

問題は、あなたが分からない月を使用しています。あなたはさらに説明する必要がM

[dateFormatter setDateFormat:@"dd-MM-yyyy"];

Unicode Date Format Patterns

4

用途:

[dateFormatter setDateFormat:@"dd-MM-yyyy"];