I have a date an article was published, but need to get how long ago it was published in relation to the current time.
So if the Article was published at 8:45AM, and it is 9:45AM on the same day, I need to be able to have a UILabel that says "1 hr ago".
Currently, I am getting the date formatted to get a date like "May 5, 2013 5:35PM":
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM d, yyyy h:mma"];
NSString *dateString = [df stringFromDate:feedLocal.published];
cell.publishedLabel.text = dateString;
}
How could I convert that to get something like "1 hr ago"? Thanks!
EDIT
Here is the current method I have to at least get the time ago:
-(NSString *)timeAgo {
NSDate *todayDate = [NSDate date];
double ti = [self timeIntervalSinceDate:todayDate];
ti = ti * -1;
if (ti < 1) {
return @"1s";
} else if (ti < 60) {
return @"1m";
} else if (ti < 3600) {
int diff = round(ti / 60);
return [NSString stringWithFormat:@"%dm", diff];
} else if (ti < 86400) {
int diff = round(ti / 60 / 60);
return[NSString stringWithFormat:@"%dh", diff];
} else if (ti < 2629743) {
int diff = round(ti / 60 / 60 / 24);
return[NSString stringWithFormat:@"%dd", diff];
} else if (ti < 31556926) {
int diff = round(ti / 60 / 60 / 24 / 30);
return [NSString stringWithFormat:@"%dmo", diff];
} else {
int diff = round(ti / 60 / 60 / 24 / 30 / 12);
return [NSString stringWithFormat:@"%dy", diff];
}
}