我正在将字符串转换为日期并再次转换为字符串。使用以下代码
NSString *newDate =[NSString stringWithFormat:@"%@",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
NSLog(@"New Date is %@",newDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy h:mm:ss a"];
NSDate *myDate = [dateFormat dateFromString:newDate];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSString *str = [dateFormat stringFromDate:myDate];
NSLog(@"DAte is %@",str);
它是在模拟器中返回日期,但在iphone 4.3.3中没有 请帮忙
答案 0 :(得分:1)
NSDateFormatter
将返回nil。
要查看它是否可以解析日期,请添加其他NSLog语句:
NSString *newDate =[NSString stringWithFormat:@"%@",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
NSLog(@"New Date is %@",newDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy h:mm:ss a"];
NSDate *myDate = [dateFormat dateFromString:newDate];
NSLog(@"Date parsed: %@", myDate);
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSString *str = [dateFormat stringFromDate:myDate];
NSLog(@"DAte is %@",str);
但是因为你只想要字符串的第一部分,为什么不只是将它串起来。
NSString *str = [newDate substringToIndex:10];
以及此处返回的内容:
NSString *newDate =[NSString stringWithFormat:@"%@",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
它的属性Date已经是NSDate了?,你能显示你的.h文件吗?