Skip to content

Instantly share code, notes, and snippets.

@ronaldmannak
Created January 4, 2013 18:24
Show Gist options
  • Save ronaldmannak/4454744 to your computer and use it in GitHub Desktop.
Save ronaldmannak/4454744 to your computer and use it in GitHub Desktop.
User friendly timestamps. https://coderwall.com/p/52edja
-(NSString*)dailyLanguage:(NSTimeInterval) overdueTimeInterval{
if (overdueTimeInterval<0)
overdueTimeInterval*=-1;
NSInteger minutes = round(overdueTimeInterval)/60;
NSInteger hours = minutes/60;
NSInteger days = hours/24;
NSInteger months = days/30;
NSInteger years = months/12;
NSString* overdueMessage;
if (years>0){
overdueMessage = [NSString stringWithFormat:@"%d %@", (years), (years==1?@"year":@"years")];
}else if (months>0){
overdueMessage = [NSString stringWithFormat:@"%d %@", (months), (months==1?@"month":@"months")];
}else if (days>0){
overdueMessage = [NSString stringWithFormat:@"%d %@", (days), (days==1?@"day":@"days")];
}else if (hours>0){
overdueMessage = [NSString stringWithFormat:@"%d %@", (hours), (hours==1?@"hour":@"hours")];
}else if (minutes>0){
overdueMessage = [NSString stringWithFormat:@"%d %@", (minutes), (minutes==1?@"minute":@"minutes")];
}else if (overdueTimeInterval<60){
overdueMessage = [NSString stringWithFormat:@"a few seconds"];
}
return overdueMessage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment