从AFNetworking3显示解析日期,以只显示通过小时

问题描述:

我想显示解析日期从AFNetworking3只显示通过小时,而不是全日,像1小时ago..etc。 我用这个来获得完整的日期从AFNetworking3显示解析日期,以只显示通过小时

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

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

+0

您需要先变换的NSString(其表示日期)转换成使用NSDateFormatter一个的NSDate对象。那么你可以看看那里https://*.com/questions/20487465/how-to-convert-nsdate-in-to-relative-format-as-today-yesterday-a-week-ago – Larme

这里是将字符串转换成的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

+0

这不应该工作或者可能给出错误的日期:应该使用“yyyy”和“HH”而不是“YYYY”和“hh”。 – Larme

+0

谢谢指出。你说得对。编辑我的答案 – pkallu