2010-11-19 10 views
3
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
[timeFormat setDateFormat:@"hh:mm a"]; 

NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"]; 
if(sourceDate==nil) 
{ 
    NSLog(@"source date nil"); 
} 

iPhoneで24時間設定を使用しています。 24時間設定に変更した後、datefromstringは常にnilを返します。私は12時間のフォーマットに変更し、再び動作します。どうして ?dateFromString変更後の返信なし24時間

答えて

5

ロケールは、24時間の設定変更の影響を受けないように設定する必要があります。
フォーマットはまた、(あなたの例では、日付を含みます)、入力文字列と一致する必要があります:

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
NSLocale *locale = [[[NSLocale alloc] 
        initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
[timeFormat setLocale:locale]; 
[timeFormat setDateFormat:@"dd/MM/yyyy hh:mm a"]; 

NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"]; 
if(sourceDate==nil) 
{ 
    NSLog(@"source date nil"); 
} 

は、より多くの情報のためQA1480を参照してください。

関連する問題