添加了此代码
NSDateFormatter *df = [[NSDateFormatter alloc]init];
NSString *entry = @"March 28, 2012";
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [df dateFromString:entry];
日期返回nil值。我对输入日期字符串有疑问。是否可以将其转换为日期?
答案 0 :(得分:4)
格式错误。请按以下步骤操作。
NSDateFormatter *df = [[NSDateFormatter alloc]init];
NSString *entry = @"March 28, 2012";
[df setDateFormat:@"MMMM dd, yyyy"];
NSDate *date = [df dateFromString:entry];
修改强>
[df setDateFormat:@"MMMM dd, yyyy HH:mm a"];
答案 1 :(得分:3)
您的日期格式字符串和日期值字符串不匹配。
无论
NSDateFormatter *df = [[NSDateFormatter alloc]init];
NSString *entry = @"2012-03-28"; //Changed this
[df setDateFormat:@"yyyy-MM-dd"]; //Kept it as yours
NSDate *date = [df dateFromString:entry];
OR
NSDateFormatter *df = [[NSDateFormatter alloc]init];
NSString *entry = @"March 28, 2012"; //Kept is as yours
[df setDateFormat:@"MMMM dd, yyyy"]; //Changed it
NSDate *date = [df dateFromString:entry];