0

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];
    }
}
Realinstomp
  • 532
  • 2
  • 13
  • 30

1 Answers1

1

Im not sure what timeAgo is a method of, but here is a solution assuming its in the same viewController as tableView:cellForRowAtIndexPath. If you can clarify what its a method of I may be able to modify this and help you more.

First change timeAgo to take in a date and do a comparison on it.

-(NSString *)timeSincePublished:(NSDate *)publicationDate 
{
    double ti = [publicationDate timeIntervalSinceNow];

Everything else should be the same in the above method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
    NSString *dateString = [self timeSincePublished:feedLocal.published];
    cell.publishedLabel.text = dateString;
}
grodier
  • 101
  • 4
  • Thanks! The method looks similar to the method I'm using to get "time ago" (added in the EDIT), but I'm not sure what to do now that I've got that part... what do you think I should do? – Realinstomp May 10 '13 at 02:27
  • what class is timeAgo a method of? – grodier May 10 '13 at 02:38
  • I created a class called timeAgo, thats where its at. Does that help? – Realinstomp May 10 '13 at 03:06
  • I edited the answer to have timeAgo (changed the name to make more sense for me) in the same viewController and takes a date to compare to the current time. Alternatively you could leave timeAgo in its current class, but have it be a class method that takes in a parameter. – grodier May 10 '13 at 03:23