Created
April 29, 2014 06:44
-
-
Save FrankWu100/70bc138b864a25226070 to your computer and use it in GitHub Desktop.
This file contains 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 *)analysisTitle:(NSString *)theDate | |
{ | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"]; | |
//[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"]; | |
//yyyy-MM-dd HH:mm:ss | |
NSDate *date = [NSDate date]; | |
date = [formatter dateFromString:theDate]; | |
// NSLog(@"Date:%@", [formatter stringFromDate:date]); | |
NSDateFormatter *dateFormatter_s = [[NSDateFormatter alloc] init]; | |
[dateFormatter_s setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_TW"]]; | |
NSDate* nowDate = [NSDate date]; | |
NSTimeInterval distanceBetweenDates = [nowDate timeIntervalSinceDate:date]; | |
//double secondsInMinute = 60; | |
//NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute; | |
// 1 分鐘內, 60 秒內 | |
// 1 小時內, 60 分鐘內, 3600 秒內 | |
// 1 天內, 24 小時內, 3600 * 24 = 86400 秒內 | |
// 1 週內, 7 天內, 86400 * 7 = 604800 秒內 | |
// 1 月內, 30~31 天內 = 前一個月 另外判斷 | |
// 1 個月外 | |
if (distanceBetweenDates <= 0) | |
{ | |
// 0 秒內 | |
// 剛剛 | |
} | |
else if (distanceBetweenDates < 60) | |
{ | |
// 1 分鐘內, 60 秒內 | |
return [[NSString alloc] initWithFormat:@"%d 秒前", (int)distanceBetweenDates]; | |
} | |
else if (distanceBetweenDates < 3600) | |
{ | |
// 1 小時內, 60 分鐘內, 3600 秒內 | |
return [[NSString alloc] initWithFormat:@"%d 分鐘前", (int)distanceBetweenDates / 60]; | |
} | |
else if (distanceBetweenDates < 86400) | |
{ | |
// 1 小時內, 60 分鐘內, 86400 秒內 | |
return [[NSString alloc] initWithFormat:@"%d 小時前", (int)distanceBetweenDates / 3600]; | |
} | |
else if (distanceBetweenDates < 604800) | |
{ | |
// 1 週內, 7 天內, 86400 * 7 = 604800 秒內 | |
[dateFormatter_s setDateFormat:@"EEEE aa h:mm"]; | |
} | |
else | |
{ | |
// 同一月 | |
[dateFormatter_s setDateFormat:@"yyyy/MM"]; | |
if ([[dateFormatter_s stringFromDate:date] isEqualToString:[dateFormatter_s stringFromDate:nowDate]]) { | |
[dateFormatter_s setDateFormat:@"M'月'd'日' aa h:mm"]; | |
} | |
else { | |
// 同一年 | |
[dateFormatter_s setDateFormat:@"yyyy"]; | |
if ([[dateFormatter_s stringFromDate:date] isEqualToString:[dateFormatter_s stringFromDate:nowDate]]) { | |
[dateFormatter_s setDateFormat:@"M'月'd'日'"]; | |
} | |
else { | |
[dateFormatter_s setDateFormat:@"y'年'M'月'd'日'"]; | |
} | |
} | |
} | |
if ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"]) { | |
//device is simulator | |
NSLog(@"Date:%@", [dateFormatter_s stringFromDate:date]); | |
} | |
return [dateFormatter_s stringFromDate:date]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment