2010-11-19 4 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 변경 후 nil 반환 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을 참조하십시오
는 형식은 (귀하의 예제에서 날짜를 포함) 입력 문자열과 일치해야합니다.