2011-06-09 31 views
1

Helo, 我正在嘗試一堆代碼,它給null。這段代碼有什麼問題嗎?NSDateFormatter @「mmddyyy」?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    // get NSDate from old string format 
    [dateFormatter setDateFormat:@"mmddyyy"]; 
    NSDate *date = [dateFormatter dateFromString:@"02012002"]; 
    NSLog(@"%@",date); 
    // get string in new date format 
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"]; 
    NSString *strMyDate= [dateFormatter stringFromDate:date]; 
    NSLog(@"%@\n\n********************************************************",strMyDate); 

輸出

2011-06-09 12:49:17.957 TestPrj[3054:207] (null) 
2011-06-09 12:49:17.959 TestPrj[3054:207] (null) 

******************************************************** 

回答

1

您④短缺的一個 'Y' 有排在第二線 - 它要麼應該是 -

dateFormatter setDateFormat:@ 「MMDDYYYY」];

或dateFormatter setDateFormat:@「MMddyyyy」];

+0

thnaks點 – 2011-06-09 07:56:03

2

請嘗試

[dateFormatter setDateFormat:@"MMddyyyy"]; 

編輯

在我以前的版本DD沒有工作在這個例子中,雖然會產生錯誤的結果開始與februar y - >回到dd

使用MM其他月份它會將其解析爲分鐘。請參考Unicode Technical Standard #35/Locale Data Markup Language (LDML)

+0

謝謝...爲了快速回復...你能告訴我爲什麼是這樣嗎? – 2011-06-09 07:28:11

+0

@Inder Kumar Rathore在帖子中解釋。 – 2011-06-09 07:50:21