2011-12-07 91 views
0

我從Facebook的用戶日期,它看起來像格式的NSString的NSDate

「birthday_date」= 「1987年3月4日」 我寫了這個代碼

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"dd/MM/YYYY"]; 
    NSDate *dateFromString = [dateFormatter dateFromString:birthDate]; 

    NSLog(@"dateformstring %@ ",dateFromString); 
    [birthPicker setDate:dateFromString animated:YES]; 

和結果日期從字符串si:1988-12-25 23:00:00 +0000,我有這個日期選擇在選擇器中,我不知道這個日期在哪裏:/

回答

3

嘗試[dateFormatter setDateFormat:@"dd/MM/yyyy"];

2

oops。正如蒂羅說的那樣,使用「yyyy」。作爲中提到的Apple docs:「在ISO年周曆使用

‘一個常見的錯誤是使用YYYY YYYY指定日曆年,而YYYY指定一年(的’創周)」

0

做了一個NSString擴展爲此。

// Simple as this. 
date = dateString.dateValue; 

由於NSDataDetector,它承認了一大堆格式。

// Tested in GMT+1 (Hungary). 
@"2014-01-16" dateValue is <2014-01-16 11:00:00 +0000> 
@"2014.01.16" dateValue is <2014-01-16 11:00:00 +0000> 
@"2014/01/16" dateValue is <2014-01-16 11:00:00 +0000> 
@"2014 Jan 16" dateValue is <2014-01-16 11:00:00 +0000> 
@"2014 Jan 16th" dateValue is <2014-01-16 11:00:00 +0000> 
@"20140116" dateValue is <2014-01-16 11:00:00 +0000> 
@"01-16-2014" dateValue is <2014-01-16 11:00:00 +0000> 
@"01.16.2014" dateValue is <2014-01-16 11:00:00 +0000> 
@"01/16/2014" dateValue is <2014-01-16 11:00:00 +0000> 
@"16 January 2014" dateValue is <2014-01-16 11:00:00 +0000> 
@"01-16-2014 17:05:05" dateValue is <2014-01-16 16:05:05 +0000> 
@"01-16-2014 T 17:05:05 UTC" dateValue is <2014-01-16 17:05:05 +0000> 
@"17:05, 1 January 2014 (UTC)" dateValue is <2014-01-01 16:05:00 +0000> 

eppz!kit部分,從GitHub搶類別NSString+EPPZKit.h