2011-07-12 53 views
2

我想將字符串轉換爲日期。已經使用了下面的代碼。但是得到錯誤的日期。String到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

回答

4

用途:

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