Advertisement
Joze

Cálculo timeago

Feb 7th, 2012
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. +(NSString *)getTimeAgo:(NSDate *)fecha
  2. {
  3.     NSString *timeAgo;
  4.     float since_mins,since_hours, since_days, since_months, since_years;
  5.  
  6.     NSTimeInterval since = fabs([fecha timeIntervalSinceNow]);
  7.     since_mins = (since / 60);
  8.     since_hours = (since_mins / 60);
  9.     since_days = (since_hours / 24);
  10.     since_months = (since_days / 31);
  11.     since_years = (since_months / 365);
  12.  
  13.     if(since_years >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_years), floor(since_years)==1?@" year ago":@" years ago"];
  14.     else if(since_months >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_months), floor(since_months)==1?@" month ago":@" months ago"];
  15.     else if(since_days >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_days), floor(since_days)==1?@" day ago":@" days ago"];
  16.     else if(since_hours >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_hours), floor(since_hours)==1?@" hour ago":@" hours ago"];
  17.     else if(since_mins >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_mins), floor(since_mins)==1?@" minute ago":@" minutes ago"];
  18.     else if(since == 60) timeAgo = @"1 minute ago";
  19.     else timeAgo = [NSString stringWithFormat:@"%.f %@", since, floor(since_mins)==1?@" second ago":@" seconds ago"];
  20.     return timeAgo;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement