2012-02-03 128 views
0

這種極其簡單的任務是apparantly很辛苦......問題轉換的NSDate成字符串

NSDate *date; 
date = [someMethod here]; 
//I've checked with debugger that the method returns an object of type NSDate. 
//The description of date at this point is: "2012-02-02 19:42:00 +0000" 

NSDateFormatter *dateFormat; 
[dateFormat setDateFormat:@"dd-MM-yy hh:mm"]; 
NSString *dateString = [dateFormat stringFromDate:date]; 

dateString只是NIL

有誰知道什麼是錯?

編輯:我真的想實現簡單地說就是:

NSString *receivedDate = @"2012-02-02T20:42:00+01:00"; 
NSString *fixedDate = [do some magic] 
//value of fixedDate is now: "02-02-12 20:42" 
+1

難道你忘了頁頭+初始化DATEFORMAT? – Anna 2012-02-03 03:23:48

+0

你是對的,謝謝! – James 2012-02-03 13:33:33

回答

3

安娜說,你需要的,因爲你得到的是一個NULL PTR,這只是忽略消息setDateFormat和stringFromDate分配NSDateFormatter的一個實例,讓你留下NULL。

但你的格式也是幾個小時是不正確的。請參閱Date Formatter reference

這個工作對我來說:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; 
[df setDateFormat:@@"dd-MM-yy HH:mm"]; 
NSString *dateString = [df stringFromDate:date]; 
+0

[sovled]在我添加alloc/init之後,它有點作品了。但它解析了小時的錯誤。 In:2012-02-03 14:20:00 +0000 推出日期:2012-02-03 02:20:00 任何人都知道爲什麼在夜間而不是一天中將14視爲2? 編輯:當然,我有......而不是HH,現在工作! – James 2012-02-03 13:34:15