更改24小时后,dateFromString返回nil [英] dateFromString return nil after change 24 hour
本文介绍了更改24小时后,dateFromString返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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小时格式,它再次运行。为什么?
I'm using 24 hour setting in iPhone. After I change to 24 hour setting, datefromstring always return nil. I rechange to 12 hour format, it's working again. Why ?
推荐答案
需要设置区域设置以避免受到24小时设置更改的影响。
格式还必须匹配输入字符串(在您的示例中包含日期):
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");
}
参见 QA1480 了解更多信息。
这篇关于更改24小时后,dateFromString返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文