Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…t out as normal date.
  • Loading branch information...
commit a6851d666f25698e133ce94b1dfa0d0d8e02816d 1 parent 767c98a
@marcosanson marcosanson authored
Showing with 20 additions and 2 deletions.
  1. +3 −1 NSDate+TimeAgo.h
  2. +17 −1 NSDate+TimeAgo.m
View
4 NSDate+TimeAgo.h
@@ -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
18 NSDate+TimeAgo.m
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.