#pragma mark时间处理
+ (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) {//时间小于一个小时
distanceStr = [NSStringstringWithFormat:@"%ld分钟前",(long)distanceTime /60];
}
else if(distanceTime <24*60*60&& [nowDayintegerValue] == [lastDay integerValue]){//时间小于一天
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"];//--设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
distanceStr = [dataFormatter stringFromDate:beDate];
}
return distanceStr;
}
调用
NSString* createdAt = [self distanceTimeWithBeforeTime:""[@"created_at"]doubleValue]];
网友评论