Created
August 4, 2014 11:24
-
-
Save ilklatte/90a53606b5c05c57ea5c to your computer and use it in GitHub Desktop.
This file contains 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
@interface NSDate (PrettyDate) | |
- (NSString *)prettyDate; | |
@end |
This file contains 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
@implementation NSDate (PrettyDate) | |
- (NSString *)prettyDate | |
{ | |
NSString * prettyTimestamp; | |
float delta = [self timeIntervalSinceNow] * -1; | |
if (delta < 60) { | |
prettyTimestamp = @"just now"; | |
} else if (delta < 120) { | |
prettyTimestamp = @"one minute ago"; | |
} else if (delta < 3600) { | |
prettyTimestamp = [NSString stringWithFormat:@"%d minutes ago", (int) floor(delta/60.0) ]; | |
} else if (delta < 7200) { | |
prettyTimestamp = @"one hour ago"; | |
} else if (delta < 86400) { | |
prettyTimestamp = [NSString stringWithFormat:@"%d hours ago", (int) floor(delta/3600.0) ]; | |
} else if (delta < ( 86400 * 2 ) ) { | |
prettyTimestamp = @"one day ago"; | |
} else if (delta < ( 86400 * 7 ) ) { | |
prettyTimestamp = [NSString stringWithFormat:@"%d days ago", (int) floor(delta/86400.0) ]; | |
} else { | |
NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; | |
[formatter setDateStyle:NSDateFormatterMediumStyle]; | |
prettyTimestamp = [NSString stringWithFormat:@"on %@", [formatter stringFromDate:self]]; | |
[formatter release]; | |
} | |
return prettyTimestamp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment