Objective c NSDate格式化程序在将NSString转换为NSDate时出现问题,其值为null

Objective c NSDate格式化程序在将NSString转换为NSDate时出现问题,其值为null,objective-c,ios4,Objective C,Ios4,大家好 在格式化程序的帮助下,我正在努力将NSString转换为NSDate,但它将为null 有人能帮忙吗 这是我的密码 NSString *dateString=[NSString stringWithFormat:@"10/26/2010/09:56:56 PM"]; NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; [dateFormat setDateFormat:@"mm/dd

大家好 在格式化程序的帮助下,我正在努力将NSString转换为NSDate,但它将为null 有人能帮忙吗 这是我的密码

 NSString *dateString=[NSString stringWithFormat:@"10/26/2010/09:56:56 PM"];
 NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormat setDateFormat:@"mm/dd/yyyy HH:mm:ss a"];
 NSDate *localDateTime =[dateFormat dateFromString:dateString];
 NSLog(@"client date is %@",localDateTime);

提前Thx可能是因为日期字符串和格式不匹配

10/26/2010/09:56:56 PM

mm/dd/yyyy HH:mm:ss a
你看到区别了吗?:)事实上,它们必须匹配。在格式中,年和小时之间有一个空格,而在实际日期中有一个斜杠。您通过“mm”匹配的月份实际上是分钟,应该是“mm”。改为:

10/26/2010 09:56:56 PM

MM/dd/yyyy HH:mm:ss a
可能会有更多的问题,所以我建议您实际查看以使其正确

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

NSDate *formatterDate = [dateFormatter dateFromString:@"1999-07-11 at 10:30"];
[dateFormatter release];
试试这个