Created
September 5, 2012 10:50
-
-
Save eternalstorms/3634928 to your computer and use it in GitHub Desktop.
String from an NSDate in the form of "2 months ago"
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 *)relativeDateStringFromDate:(NSDate *)date | |
{ | |
NSDate *now = [NSDate date]; | |
if ([now laterDate:date] == date) | |
return nil; //dates in the future are not supported | |
NSInteger years = [now yearsSinceDate:date]; | |
NSInteger months = [now monthsSinceDate:date]; | |
NSInteger weeks = [now weeksSinceDate:date]; | |
NSInteger days = [now daysSinceDate:date]; | |
NSInteger hours = [now hoursSinceDate:date]; | |
NSInteger minutes = [now minutesSinceDate:date]; | |
NSInteger seconds = [now secondsSinceDate:date]; | |
if (years > 0) | |
{ | |
if (years == 1) | |
return [NSString stringWithFormat:NSLocalizedString(@"commentyearago",nil),years]; | |
else | |
return [NSString stringWithFormat:NSLocalizedString(@"commentyearsago",nil),years]; | |
} else if (months > 0) | |
{ | |
if (months == 1) | |
return [NSString stringWithFormat:NSLocalizedString(@"commentmonthago",nil),months]; | |
else | |
return [NSString stringWithFormat:NSLocalizedString(@"commentmonthsago",nil),months]; | |
} else if (weeks > 0) | |
{ | |
if (weeks == 1) | |
return [NSString stringWithFormat:NSLocalizedString(@"commentweekago",nil),weeks]; | |
else | |
return [NSString stringWithFormat:NSLocalizedString(@"commentweeksago",nil),weeks]; | |
} else if (days > 0) | |
{ | |
if (days == 1) | |
return [NSString stringWithFormat:NSLocalizedString(@"commentdayago",nil),days]; | |
else | |
return [NSString stringWithFormat:NSLocalizedString(@"commentdaysago",nil),days]; | |
} else if (hours > 0) | |
{ | |
if (hours == 1) | |
return [NSString stringWithFormat:NSLocalizedString(@"commenthourago",nil),hours]; | |
else | |
return [NSString stringWithFormat:NSLocalizedString(@"commenthoursago",nil),hours]; | |
} else if (minutes > 0) | |
{ | |
if (minutes == 1) | |
return [NSString stringWithFormat:NSLocalizedString(@"commentminuteago",nil),minutes]; | |
else | |
return [NSString stringWithFormat:NSLocalizedString(@"commentminutesago",nil),minutes]; | |
} else if (seconds > 0) | |
{ | |
if (seconds == 1) | |
return [NSString stringWithFormat:NSLocalizedString(@"commentsecondago",nil),seconds]; | |
else | |
return [NSString stringWithFormat:NSLocalizedString(@"commentsecondsago",nil),seconds]; | |
} else | |
return NSLocalizedString(@"justnowcomment",nil); | |
return @""; | |
} |
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
"commentyearago" = "%ld year ago"; | |
"commentyearsago" = "%ld years ago"; | |
"commentmonthago" = "%ld month ago"; | |
"commentmonthsago" = "%ld months ago"; | |
"commentweekago" = "%ld week ago"; | |
"commentweeksago" = "%ld weeks ago"; | |
"commentdayago" = "%ld day ago"; | |
"commentdaysago" = "%ld days ago"; | |
"commenthourago" = "%ld hour ago"; | |
"commenthoursago" = "%ld hours ago"; | |
"commentminuteago" = "%ld minute ago"; | |
"commentminutesago" = "%ld minutes ago"; | |
"commentsecondago" = "%ld second ago"; | |
"commentsecondsago" = "%ld seconds ago"; | |
"justnowcomment" = "just now"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment