2012-02-06 92 views
2

我想獲得一個字符串日期到一個日期對象,所以我可以做一些比較。我的代碼如下:NSDateFormatter dateFromString回來null

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"yyyy/MM/dd_hh:mm:ss"]; 
NSDate *date = [formatter dateFromString:@"2012/02/06_20:35:59"]; 
[formatter release]; 

NSLog(@"date = %@", date); 

我的NSLog顯示* date = null;

任何幫助表示讚賞。謝謝。

回答

3
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSDate *date = [formatter dateFromString:@"2012-02-06 20:35:59"]; 
    [formatter release]; 

    NSLog(@"date = %@", date); 


d Day of the month as digits; no leading zero for single-digit days. 
dd Day of the month as digits; leading zero for single-digit days. 
ddd Day of the week as a three-letter abbreviation. 
dddd Day of the week as its full name. 
m Month as digits; no leading zero for single-digit months. 
mm Month as digits; leading zero for single-digit months. 
mmm Month as a three-letter abbreviation. 
mmmm Month as its full name. 
yy Year as last two digits; leading zero for years less than 10. 
yyyy Year represented by four digits. 
h Hours; no leading zero for single-digit hours (12-hour clock). 
hh Hours; leading zero for single-digit hours (12-hour clock). 
H Hours; no leading zero for single-digit hours (24-hour clock). 
HH Hours; leading zero for single-digit hours (24-hour clock). 
M Minutes; no leading zero for single-digit minutes. 
Uppercase M unlike CF timeFormat‘s m to avoid conflict with months. 
MM Minutes; leading zero for single-digit minutes. 
Uppercase MM unlike CF timeFormat‘s mm to avoid conflict with months. 
s Seconds; no leading zero for single-digit seconds. 
ss Seconds; leading zero for single-digit seconds. 
相關問題