Created
March 22, 2018 10:22
-
-
Save futuretap/e02bd48052e5946787549b18d5a53a99 to your computer and use it in GitHub Desktop.
relative time formatter
This file contains hidden or 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
- (NSString*)localizedRelativeTime { | |
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init]; | |
formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute; | |
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleShort; | |
if (self.timeIntervalSinceNow < 60 && self.timeIntervalSinceNow > -60) { | |
return NSLocalizedString(@"right now", @"from MapKit/Maps"); | |
} else if (self.timeIntervalSinceNow > 0) { | |
NSString *start = [formatter stringFromDate:NSDate.date toDate:self]; | |
return [NSString stringWithFormat:NSLocalizedString(@"in %@", @"placeholder: minutes/hours"), start]; | |
} else { | |
NSString *ago = [formatter stringFromDate:self toDate:NSDate.date]; | |
return [NSString stringWithFormat:NSLocalizedString(@"%@ ago", @"placeholder: minutes/hours"), ago]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment