我正在获取一系列值,包括来自服务器的日期。当我在tableview中显示日期时,我需要更改格式。所以这是我的数组:
{
amount = "-651.5700";
"date_added" = "2014-04-14 03:03:10";
description = "Order Amount Paid ";
"transaction_id" = 795;
}, {
amount = "-1985.0700";
"date_added" = "2014-04-14 03:03:10";
description = "Order Amount Paid ";
"transaction_id" = 794;
}
我用cellForRowAtIndexPath
方法在表格视图中显示日期的代码是:
NSString *str=[[[ArrDashboard objectAtIndex:indexPath.row] objectForKey:@"date_added"] copy];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormatter dateFromString:str];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *strMyDate= [dateFormatter stringFromDate:date];
cell.LblDate.text = [NSString stringWithFormat:@"%@",strMyDate];
我在数组中得到很多值,但在转换后,其中一些值为null,有些是转换后的日期字符串 如果有人可以提供帮助,那将会很有用 谢谢。
答案 0 :(得分:0)
NSString *str=[[[ArrDashboard objectAtIndex:indexPath.row] objectForKey:@"date_added"] copy];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:str];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *strMyDate= [dateFormatter stringFromDate:date];
cell.LblDate.text = [NSString stringWithFormat:@"%@",strMyDate];
您的日期格式对于您从回复中提取的日期格式是错误的
答案 1 :(得分:-1)
Alloc you dateformatter试试这段代码完美无缺
NSString *str=@"2014-04-14 03:03:10";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormatter dateFromString:str];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *strMyDate= [dateFormatter stringFromDate:date];
NSString *strResult = [NSString stringWithFormat:@"%@",strMyDate];