0

I am trying to display parsed date from AFNetworking3 to display only passed hours, not full date, like 1 hour ago..etc. I used this to get the full date

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

This is the link of json data: http://mkssab.com/api/index

ema so
  • 119
  • 1
  • 7
  • You need to first transform the NSString (which represent a date) into a NSDate object using a NSDateFormatter. Then you can look there https://stackoverflow.com/questions/20487465/how-to-convert-nsdate-in-to-relative-format-as-today-yesterday-a-week-ago – Larme Jul 02 '17 at 13:35

1 Answers1

0

Here is the code for converting string into NSDate and calculating the difference b/w current date and date.

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

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

pkallu
  • 86
  • 8
  • That shouldn't work or maybe gives wrong dates: "yyyy" and "HH" should be used instead of "YYYY" and "hh". – Larme Jul 02 '17 at 16:26