Created
December 3, 2010 02:51
-
-
Save jarodl/726508 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
#import <Foundation/Foundation.h> | |
@interface NSString (Extensions) | |
+ (NSString *)timeAgoStringFromDate:(NSDate *)fromDate; | |
@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
#import "NSString+Extensions.h" | |
#define FormatterString @"%i %@ ago" | |
@implementation NSString (Extensions) | |
+ (NSString *)timeAgoStringFromDate:(NSDate *)fromDate | |
{ | |
NSUInteger desiredComponents = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; | |
NSDateComponents *elapsedTime = [[NSCalendar currentCalendar] components:desiredComponents | |
fromDate:fromDate | |
toDate:[NSDate date] | |
options:0]; | |
NSString *timeAgo = nil; | |
if ([elapsedTime year]) { | |
timeAgo = [NSString stringWithFormat:FormatterString, [elapsedTime year], | |
([elapsedTime year] == 1) ? @"year" : @"years"]; | |
} | |
else if ([elapsedTime month]) { | |
timeAgo = [NSString stringWithFormat:FormatterString, [elapsedTime month], | |
([elapsedTime month] == 1) ? @"month" : @"months"]; | |
} | |
else if ([elapsedTime week]) { | |
timeAgo = [NSString stringWithFormat:FormatterString, [elapsedTime week], | |
([elapsedTime week] == 1) ? @"week" : @"weeks"]; | |
} | |
else if ([elapsedTime day]) { | |
timeAgo = [NSString stringWithFormat:FormatterString, [elapsedTime day], | |
([elapsedTime day] == 1) ? @"day" : @"days"]; | |
} | |
else if ([elapsedTime hour]) { | |
timeAgo = [NSString stringWithFormat:FormatterString, [elapsedTime hour], | |
([elapsedTime hour] == 1) ? @"hour" : @"hours"]; | |
} | |
else if ([elapsedTime minute]) { | |
timeAgo = [NSString stringWithFormat:FormatterString, [elapsedTime minute], | |
([elapsedTime minute] == 1) ? @"minute" : @"minutes"]; | |
} | |
else { | |
timeAgo = [NSString stringWithFormat:FormatterString, [elapsedTime second], | |
([elapsedTime second] == 1) ? @"second" : @"seconds"]; | |
} | |
return timeAgo; | |
} | |
@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
// set the time zone of the date value used | |
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; | |
// To set the time zone of the calendar that we use to get the elapsed time, do this: | |
NSCalendar *cal = [NSCalendar currentCalendar]; | |
[cal setTimeZone:[NSTimeZone localTimeZone]]; | |
NSDateComponents *elapsedTime = [cal components:desiredComponents fromDate:fromDate toDate:[NSDate date] options:0]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment