2013-05-31 77 views
-1

你好,我已經與解析這個NSStringNSDate奇怪的問題: 「2013-05-31T11:56:07+0200」,I`ve嘗試:NSDateFormatter迄今

[_dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; 
[_dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; 

但總是返回爲零。

+1

顯示您的完整代碼。有了這個dateFormat它應該工作。 – Anupdas

+0

還有其他問題。你確定你的日期格式化程序甚至存在嗎? –

回答

1

從適用於iOS 5.1及以上的這些指定的unicode日期格式的時區格式

enter image description here

我已經驗證了這一個,並與輸出工作2013年5月31日9時56分07秒+0000

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; 
NSDate *date = [dateFormatter dateFromString:@"2013-05-31T11:56:07+0200"]; 
NSLog(@"%@",date); 
+0

Hm return again nil :( – user2429398

+0

@ user2429398你可以在你的問題中發佈完整的代碼和轉換嗎? – Anupdas

+0

爲什麼在結尾處額外的'''字符? –

0

它可以幫助你

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyy-MM-dd"]; 

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
[timeFormat setDateFormat:@"HH:mm:ss"]; 

NSDate *now = [[NSDate alloc] init]; 

NSString *theDate = [dateFormat stringFromDate:now]; 
NSString *theTime = [timeFormat stringFromDate:now]; 

NSLog(@"\n" 
     "theDate: |%@| \n" 
     "theTime: |%@| \n" 
     , theDate, theTime);