dateFromString return nil after change 24 hour

北战南征 提交于 2019-12-01 09:50:42

The locale needs to be set to avoid being affected by the 24-hour setting change.
The format also has to match the input string (which in your example includes the date):

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");
}

See QA1480 for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!