Permalink
Browse files

first commit

  • Loading branch information...
kevinlawler committed Nov 13, 2011
0 parents commit 00e827ee5228ab45ee19269dc31e9439e959f8d3
Showing with 45 additions and 0 deletions.
  1. +4 −0 NSDate+TimeAgo.h
  2. +41 −0 NSDate+TimeAgo.m
  3. 0 README.md
@@ -0,0 +1,4 @@
+@interface NSDate (TimeAgo)
+-(NSString *)timeAgo;
+@end
+
@@ -0,0 +1,41 @@
+#import "NSDate+TimeAgo.h"
+
+@implementation NSDate (TimeAgo)
+
+
+-(NSString *)timeAgo {
+ NSDate *now = [NSDate date];
+ double deltaSeconds = fabs([self timeIntervalSinceDate:now]);
+ double deltaMinutes = deltaSeconds / 60.0f;
+
+ if(deltaSeconds < 5) {
+ return @"Just now";
+ } else if(deltaSeconds < 60) {
+ return [NSString stringWithFormat:@"%d seconds ago", (int)deltaSeconds];
+ } else if(deltaSeconds < 120) {
+ return @"A minute ago";
+ } else if (deltaMinutes < 60) {
+ return [NSString stringWithFormat:@"%d minutes ago", (int)deltaMinutes];
+ } else if (deltaMinutes < 120) {
+ return @"An hour ago";
+ } else if (deltaMinutes < (24 * 60)) {
+ return [NSString stringWithFormat:@"%d hours ago", (int)floor(deltaMinutes/60)];
+ } else if (deltaMinutes < (24 * 60 * 2)) {
+ return @"Yesterday";
+ } else if (deltaMinutes < (24 * 60 * 7)) {
+ return [NSString stringWithFormat:@"%d days ago", (int)floor(deltaMinutes/(60 * 24))];
+ } else if (deltaMinutes < (24 * 60 * 14)) {
+ return @"Last week";
+ } else if (deltaMinutes < (24 * 60 * 31)) {
+ return [NSString stringWithFormat:@"%d weeks ago", (int)floor(deltaMinutes/(60 * 24 * 7))];
+ } else if (deltaMinutes < (24 * 60 * 61)) {
+ return @"Last month";
+ } else if (deltaMinutes < (24 * 60 * 365.25)) {
+ return [NSString stringWithFormat:@"%d months ago", (int)floor(deltaMinutes/(60 * 24 * 30))];
+ } else if (deltaMinutes < (24 * 60 * 731)) {
+ return @"Last year";
+ }
+ return [NSString stringWithFormat:@"%d years ago", (int)floor(deltaMinutes/(60 * 24 * 365))];
+}
+
+@end
No changes.

0 comments on commit 00e827e

Please sign in to comment.