起始本人使用的是以下格式输出时间显示的
- (NSString *) compareCurrentTime:(NSString *)str
{
//把字符串转为NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timeDate = [dateFormatter dateFromString:str];
//得到与当前时间差
NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
timeInterval = -timeInterval;
long temp = 0;
NSString *result;
if (timeInterval < 60) {
result = [NSString stringWithFormat:@"刚刚"];
}
else if((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%ld分钟前",temp];
}
else if((temp = temp/60) <24){
result = [NSString stringWithFormat:@"%ld小时前",temp];
}
else if((temp = temp/24) <30){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
else if((temp = temp/30) <12){
result = [NSString stringWithFormat:@"%ld月前",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
}
之后查找资料后发现一种更好的时间格式显示效果,便记录于此。原文于此
- (NSString *)format:(NSString *)string{
NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate*inputDate = [inputFormatter dateFromString:string];
//NSLog(@"startDate= %@", inputDate);
NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//get date str
NSString *str= [outputFormatter stringFromDate:inputDate];
//str to nsdate
NSDate *strDate = [outputFormatter dateFromString:str];
//修正8小时的差时
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: strDate];
NSDate *endDate = [strDate dateByAddingTimeInterval: interval];
//NSLog(@"endDate:%@",endDate);
NSString *lastTime = [self compareDate:endDate];
NSLog(@"lastTime = %@",lastTime);
return lastTime;
}
-(NSString *)compareDate:(NSDate *)date{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
//修正8小时之差
NSDate *date1 = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date1];
NSDate *localeDate = [date1 dateByAddingTimeInterval: interval];
//NSLog(@"nowdate=%@\nolddate = %@",localeDate,date);
NSDate *today = localeDate;
NSDate *yesterday,*beforeOfYesterday;
//今年
NSString *toYears;
toYears = [[today description] substringToIndex:4];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
beforeOfYesterday = [yesterday dateByAddingTimeInterval: -secondsPerDay];
// 10 first characters of description is the calendar date:
NSString *todayString = [[today description] substringToIndex:10];
NSString *yesterdayString = [[yesterday description] substringToIndex:10];
NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];
NSString *dateString = [[date description] substringToIndex:10];
NSString *dateYears = [[date description] substringToIndex:4];
NSString *dateContent;
if ([dateYears isEqualToString:toYears]) {//同一年
//今 昨 前天的时间
NSString *time = [[date description] substringWithRange:(NSRange){11,5}];
//其他时间
NSString *time2 = [[date description] substringWithRange:(NSRange){5,11}];
if ([dateString isEqualToString:todayString]){
dateContent = [NSString stringWithFormat:@"今天 %@",time];
return dateContent;
} else if ([dateString isEqualToString:yesterdayString]){
dateContent = [NSString stringWithFormat:@"昨天 %@",time];
return dateContent;
}else if ([dateString isEqualToString:beforeOfYesterdayString]){
dateContent = [NSString stringWithFormat:@"前天 %@",time];
return dateContent;
}else{
return time2;
}
}else{
return dateString;
}
}