Skip to content

Instantly share code, notes, and snippets.

@jonahsiegle
Created April 30, 2014 16:37
Show Gist options
  • Save jonahsiegle/054f36dca9f961dd3d59 to your computer and use it in GitHub Desktop.
Save jonahsiegle/054f36dca9f961dd3d59 to your computer and use it in GitHub Desktop.
Converts NSTimeInterval into a verbal NSString format.
- (NSString *)readableTimeWithDuration:(NSTimeInterval)timeDuration{
NSUInteger roundedTime = round(timeDuration);
unsigned long days = roundedTime / 86400;
unsigned long hours = roundedTime / 3600;
unsigned long minutes = (roundedTime / 60) % 60;
NSString *dayLabel = days > 1 ? @"Days" : @"Day";
NSString *hourLabel = hours > 1 ? @"Hours" : @"Hour";
NSString *minuteLabel = minutes > 1 ? @"Minutes" : @"Minute";
NSString *string = (days>0) ? [NSString stringWithFormat:@"%lu %@, %lu %@",
days, dayLabel, hours, hourLabel] : [NSString stringWithFormat:@"%lu %@, %lu %@",
hours, hourLabel, minutes, minuteLabel];
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment