显示AFNetworking3中的已解析日期,仅显示已传递的小时数

时间:2017-07-02 07:43:46

标签: ios objective-c json ios10

我正在尝试从AFNetworking3显示解析日期,以显示仅传递的小时数,而不是完整日期,如1小时前...等。 我用它来获取完整的日期

    allData = [responseObject objectForKey:@"data"];
    NSDictionary *data = [allData objectAtIndex:indexPath.row];
    cell.created_at.text = [data objectForKey:@"created_at"];

这是json数据的链接: http://mkssab.com/api/index

1 个答案:

答案 0 :(得分:0)

以下是将字符串转换为NSDate并计算差异b / w当前日期和日期的代码。

//This piece of code get the date from created date
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* createdOn = [formatter dateFromString:[data objectForKey:@"created_at"]];

//This piece of code calculates the difference b/w created date and current date.
NSTimeInterval interval = [createdOn timeIntervalSinceDate:NSDate.date];
NSInteger hours = interval/3600;

参考:https://developer.apple.com/documentation/foundation/nsdate?language=objc