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 小时格式,它又可以工作了。为什么 ?
需要设置区域设置以避免受到 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");
}
See QA1480了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)