sat*_*god 3 nsdate nsformatter ios4
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");
}
Run Code Online (Sandbox Code Playgroud)
我在iPhone上使用24小时设置.更改为24小时设置后,datefromstring始终返回nil.我改为12小时格式,它再次运行.为什么?
小智 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");
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅QA1480.