Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在ios 4.3.3中,NSDateFormatter为日期返回null_Iphone_Nsdateformatter - Fatal编程技术网

Iphone 在ios 4.3.3中,NSDateFormatter为日期返回null

Iphone 在ios 4.3.3中,NSDateFormatter为日期返回null,iphone,nsdateformatter,Iphone,Nsdateformatter,我正在将字符串转换为日期,然后再次转换为字符串。使用以下代码 NSString *newDate =[NSString stringWithFormat:@"%@",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]]; NSLog(@"New Date is %@",newDate); NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

我正在将字符串转换为日期,然后再次转换为字符串。使用以下代码

 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);
它在模拟器中返回日期,但在iPhone4.3.3中不返回
请帮助

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]];

如果属性日期已经是NSDate?,能否显示作业的.h文件?

是。。我在UITableviewcell[cell setJobDate:[NSString stringWithFormat:@“%@”,str]]中有标签;新日期是2011年7月14日凌晨3:49:34这是来自webservice我想从中删除时间..这在设备中是正确的。但在转换后,它返回null2011-09-12 16:32:40.612 ITClassifiedUniversal[2802:607]新日期是2011年6月2日7:12:32 AM 2011-09-12 16:32:40.617 ITClassifiedUniversal[2802:607]解析日期:(null)2011-09-12 16:32:40.618 ITClassifiedUniversal[2802:607]日期是(null)您可以看到解析的日期是null,因为您的格式字符串不正确。但它仅适用于设备。。我该怎么办