ios - Get NSDate to say "XX time ago" that article was published -


i have date article published, need how long ago published in relation current time.

so if article published @ 8:45am, , 9:45am on same day, need able have uilabel says "1 hr ago".

currently, getting date formatted date "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 convert "1 hr ago"? thanks!

edit

here current method have @ least 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];     } } 

im not sure timeago method of, here solution assuming in same viewcontroller tableview:cellforrowatindexpath. if can clarify method of may able modify , more.

first change timeago take in date , comparison on it.

-(nsstring *)timesincepublished:(nsdate *)publicationdate  {     double ti = [publicationdate timeintervalsincenow]; 

everything else should same in 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; } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -