2014-01-24 147 views
-2

我必須將2014年1月24日下午7:27:56轉換爲NSDate ,但由於大寫PM/AM而面臨將其轉換的問題,我正在使用此代碼,但它給了我零。將NSString轉換爲NSDate

NSString *stringDate = @"Jan 24, 2014, 7:27:56 PM"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"]; 
NSDate *date=[dateFormatter dateFromString:stringDate]; 
NSLog(@"Date 1 : %@",date); //2013-02-28 12:00:00 +0000 
+0

你有什麼辦法試圖改變你的輸入到正確的格式,或者改變你的日期格式化程序來接受當前形式的輸入? –

+0

您還需要將格式化程序的區域設置設置爲'en_US_POSIX'。如果你不這樣做,你的代碼將不能在使用非英語語言的設備上工作。 – rmaddy

回答

3

您的格式器字符串包含序列「yyyy hh」,但stringDate具有「yyyy,hh」。試試這個:

[dateFormatter setDateFormat:@"MMM dd, yyyy, hh:mm:ss a"];