2013-10-18 76 views
-3

我想將字符串轉換爲日期,但它返回null。Nsdate轉換返回空

NSString *offer_publishdate*[email protected]"15/10/2013"; 
     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setDateFormat:@"yyyyMMdd"]; 
     NSDate *date = [dateFormat dateFromString:offer_publishdate]; 
      NSLog(@"date:%@",date); 
+3

請在發佈此類問題之前在iOS開發者庫中閱讀它[鏈接](https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html) – falsecrypt

+0

爲什麼在世界是否會嘗試使用與日期格式不匹配的日期格式? –

+0

首先我給出了正確的格式,後來我發現像這樣的東西 – user2702179

回答

0

嘗試這樣....

NSString *offer_publishdate*[email protected]"15/10/2013"; 
     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setDateFormat:@"dd/MM/yyyy"]; 
     NSDate *date = [dateFormat dateFromString:offer_publishdate]; 
+0

更改日期格式。 – user1673099

+0

爲什麼低估我?我是否給出了錯誤的答案? – user1673099

+0

我還沒有投票給你 – user2702179

1

請選擇確切的日期格式

NSString *offer_publishdate*[email protected]"15/10/2013"; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd/MM/yyyy"]; 
    NSDate *date = [dateFormat dateFromString:offer_publishdate]; 
     NSLog(@"date:%@",date);