Skip to content

Instantly share code, notes, and snippets.

@futuretap
Created March 22, 2018 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futuretap/e02bd48052e5946787549b18d5a53a99 to your computer and use it in GitHub Desktop.
Save futuretap/e02bd48052e5946787549b18d5a53a99 to your computer and use it in GitHub Desktop.
relative time formatter
- (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