#pragma mark時(shí)間處理
+ (NSString*)distanceTimeWithBeforeTime:(double)beTime {
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
double distanceTime = now - beTime;
NSString* distanceStr;
NSDate* beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
NSDateFormatter* dataFormatter = [[NSDateFormatter alloc] init];
[dataFormatter setDateFormat:@"HH:mm"];
NSString* timeStr = [dataFormatter stringFromDate:beDate];
[dataFormatter setDateFormat:@"dd"];
NSString* nowDay = [dataFormatter stringFromDate:[NSDatedate]];
NSString* lastDay = [dataFormatter stringFromDate:beDate];
if(distanceTime <60) {//小于一分鐘
distanceStr =@"剛剛";
}
elseif(distanceTime <60*60) {//時(shí)間小于一個(gè)小時(shí)
distanceStr = [NSStringstringWithFormat:@"%ld分鐘前",(long)distanceTime /60];
}
else if(distanceTime <24*60*60&& [nowDayintegerValue] == [lastDay integerValue]){//時(shí)間小于一天
distanceStr = [NSString stringWithFormat:@"今天%@",timeStr];
}
elseif(distanceTime<24*60*60*2&& [nowDayintegerValue] != [lastDay integerValue]){
if([nowDay integerValue] - [lastDay integerValue] ==1|| ([lastDay integerValue] - [nowDay integerValue] >10&& [nowDay integerValue] ==1)) {
distanceStr = [NSString stringWithFormat:@"昨天%@",timeStr];
}
else{
[dataFormatter setDateFormat:@"MM-dd HH:mm"];
distanceStr = [dataFormatter stringFromDate:beDate];
}
}
elseif(distanceTime <24*60*60*365){
[dataFormatter setDateFormat:@"MM-dd HH:mm"];
distanceStr = [dataFormatter stringFromDate:beDate];
}
else{
[dataFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];//--設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
distanceStr = [dataFormatter stringFromDate:beDate];
}
return distanceStr;
}
調(diào)用
NSString* createdAt = [self distanceTimeWithBeforeTime:""[@"created_at"]doubleValue]];