2014-12-27 114 views
0

我試圖將字符串轉換爲NSdate。字串的日期是「2014年12月27日上午07時42分00秒+0000iOS將NSString轉換爲NSDate總是零

這裏是我的代碼:

NSDate *date =[[NSDate alloc] init]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss a"; 
date = [ dateFormatter dateFromString:dateString]; 

的日期值始終爲零。你們中的任何一個人都知道我能做什麼錯了?

我會真正appreaciate你的幫助。

+0

我們展示的字符串。 – gnasher729 2014-12-27 18:41:55

+5

[[NSDate alloc] init]完全沒有意義。 – gnasher729 2014-12-27 18:42:37

回答

8

嘗試

dateFormatter.dateFormat = @"MM/dd/yyyy hh:mm:ss a Z"; 

HH是24小時和衝突aZ代表時區偏移量。

對符一看:http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"MM/dd/yyyy hh:mm:ss a Z"; 
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
// see https://developer.apple.com/library/ios/qa/qa1480/_index.html 


NSDate *date = [dateFormatter dateFromString:dateString]; 
7
NSString *str [email protected]"12/27/2014 07:42:00 AM +0000";   
    NSDateFormatter *sdateFormatter = [[NSDateFormatter alloc] init]; 
    sdateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
    [sdateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a Z'"]; 

    NSDate *sdate = [sdateFormatter dateFromString:str]; 
    NSLog(@"%@",[sdateFormatter stringFromDate:sdate]);