Permalink
Browse files

timeAgoWithLimit so for example date older than a week should be prin…

…t out as normal date.
  • Loading branch information...
1 parent 767c98a commit a6851d666f25698e133ce94b1dfa0d0d8e02816d @marcosanson marcosanson committed Nov 27, 2012
Showing with 20 additions and 2 deletions.
  1. +3 −1 NSDate+TimeAgo.h
  2. +17 −1 NSDate+TimeAgo.m
View
@@ -1,4 +1,6 @@
@interface NSDate (TimeAgo)
--(NSString *)timeAgo;
+- (NSString *) timeAgo;
+- (NSString *) timeAgoWithLimit:(NSTimeInterval)limit;
+- (NSString *) timeAgoWithLimit:(NSTimeInterval)limit dateFormat:(NSDateFormatterStyle)dFormatter andTimeFormat:(NSDateFormatterStyle)tFormatter;
@end
View
@@ -7,7 +7,8 @@ @implementation NSDate (TimeAgo)
NSLocalizedStringFromTable(key, @"NSDateTimeAgo", nil)
#endif
--(NSString *)timeAgo {
+- (NSString *)timeAgo
+{
NSDate *now = [NSDate date];
double deltaSeconds = fabs([self timeIntervalSinceDate:now]);
double deltaMinutes = deltaSeconds / 60.0f;
@@ -43,4 +44,19 @@ -(NSString *)timeAgo {
return [NSString stringWithFormat:NSDateTimeAgoLocalizedStrings(@"%d years ago"), (int)floor(deltaMinutes/(60 * 24 * 365))];
}
+- (NSString *) timeAgoWithLimit:(NSTimeInterval)limit
+{
+ return [self timeAgoWithLimit:limit dateFormat:NSDateFormatterFullStyle andTimeFormat:NSDateFormatterFullStyle];
+}
+
+- (NSString *) timeAgoWithLimit:(NSTimeInterval)limit dateFormat:(NSDateFormatterStyle)dFormatter andTimeFormat:(NSDateFormatterStyle)tFormatter
+{
+ if (fabs([self timeIntervalSinceDate:[NSDate date]]) <= limit)
+ return [self timeAgo];
+
+ return [NSDateFormatter localizedStringFromDate:self
+ dateStyle:dFormatter
+ timeStyle:tFormatter];
+}
+
@end

0 comments on commit a6851d6

Please sign in to comment.