參考github源碼中的一個(gè)格式化字符串的源碼,
是NSDate的一個(gè)擴(kuò)展方法:
- /**
- * Given the reference date and return a pretty date string to show
- *
- * @param refrence the date to refrence
- *
- * @return a pretty date string, like "just now", "1 minute ago", "2 weeks ago", etc
- */
- - (NSString *)prettyDateWithReference:(NSDate *)reference {
- NSString *suffix = @"ago";
-
- float different = [reference timeIntervalSinceDate:self];
- if (different < 0) {
- different = -different;
- suffix = @"from now";
- }
-
- // days = different / (24 * 60 * 60), take the floor value
- float dayDifferent = floor(different / 86400);
-
- int days = (int)dayDifferent;
- int weeks = (int)ceil(dayDifferent / 7);
- int months = (int)ceil(dayDifferent / 30);
- int years = (int)ceil(dayDifferent / 365);
-
- // It belongs to today
- if (dayDifferent <= 0) {
- // lower than 60 seconds
- if (different < 60) {
- return @"just now";
- }
-
- // lower than 120 seconds => one minute and lower than 60 seconds
- if (different < 120) {
- return [NSString stringWithFormat:@"1 minute %@", suffix];
- }
-
- // lower than 60 minutes
- if (different < 660 * 60) {
- return [NSString stringWithFormat:@"%d minutes %@", (int)floor(different / 60), suffix];
- }
-
- // lower than 60 * 2 minutes => one hour and lower than 60 minutes
- if (different < 7200) {
- return [NSString stringWithFormat:@"1 hour %@", suffix];
- }
-
- // lower than one day
- if (different < 86400) {
- return [NSString stringWithFormat:@"%d hours %@", (int)floor(different / 3600), suffix];
- }
- }
- // lower than one week
- else if (days < 7) {
- return [NSString stringWithFormat:@"%d day%@ %@", days, days == 1 ? @"" : @"s", suffix];
- }
- // lager than one week but lower than a month
- else if (weeks < 4) {
- return [NSString stringWithFormat:@"%d week%@ %@", weeks, weeks == 1 ? @"" : @"s", suffix];
- }
- // lager than a month and lower than a year
- else if (months < 12) {
- return [NSString stringWithFormat:@"%d month%@ %@", months, months == 1 ? @"" : @"s", suffix];
- }
- // lager than a year
- else {
- return [NSString stringWithFormat:@"%d year%@ %@", years, years == 1 ? @"" : @"s", suffix];
- }
-
- return self.description;
- }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。