Created
September 4, 2012 08:39
-
-
Save jlcampana/3618589 to your computer and use it in GitHub Desktop.
Time ago
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(NSString *)getTimeAgo:(NSDate *)fecha | |
{ | |
NSString *timeAgo; | |
float since_mins,since_hours, since_days, since_months, since_years; | |
NSTimeInterval since = fabs([fecha timeIntervalSinceNow]); | |
since_mins = (since / 60); | |
since_hours = (since_mins / 60); | |
since_days = (since_hours / 24); | |
since_months = (since_days / 31); | |
since_years = (since_months / 365); | |
if(since_years >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_years), floor(since_years)==1?@" year ago":@" years ago"]; | |
else if(since_months >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_months), floor(since_months)==1?@" month ago":@" months ago"]; | |
else if(since_days >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_days), floor(since_days)==1?@" day ago":@" days ago"]; | |
else if(since_hours >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_hours), floor(since_hours)==1?@" hour ago":@" hours ago"]; | |
else if(since_mins >= 1) timeAgo = [NSString stringWithFormat:@"%.f %@",floor(since_mins), floor(since_mins)==1?@" min ago":@" min ago"]; | |
else if(since == 59) timeAgo = @"1 min ago"; | |
else timeAgo = [NSString stringWithFormat:@"%.f %@", since, floor(since_mins)==1?@" sec ago":@" sec ago"]; | |
return timeAgo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment