Advertisement
Guest User

Untitled

a guest
May 27th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. - (NSString *)timeAgo:(NSDate *)compareDate{
  2.  
  3.  
  4. // NSDate *dd=@2015-05-27 11:15:05 +0000;
  5.  
  6. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  7. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
  8. NSDate *date = [dateFormatter dateFromString:@"2015-05-27 11:15:05 +0000"];
  9.  
  10. NSTimeInterval interval = [date timeIntervalSinceReferenceDate];
  11. interval = -[compareDate timeIntervalSinceNow];
  12. int temp = 0;
  13. NSString *result;
  14. if (interval < 60) {
  15. result = [NSString stringWithFormat:@"Just now"]; //less than a minute
  16. }else if((temp = interval/60) <60){
  17. result = [NSString stringWithFormat:@"%dm",temp]; //minutes ago
  18. }else if((temp = temp/60) <24){
  19. result = [NSString stringWithFormat:@"%dh",temp]; //hours ago
  20. }else{
  21. temp = temp / 24;
  22. result = [NSString stringWithFormat:@"%dd",temp]; //days ago
  23. }
  24. return result;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement