美文网首页
iOS 关于评论时间的判断

iOS 关于评论时间的判断

作者: 朋友一生 | 来源:发表于2022-06-25 10:28 被阅读0次

- (NSString*)distanceTimeWithBeforeTime:(double)beTime

{

   NSTimeIntervalnow = [[NSDatedate]timeIntervalSince1970];

   doubledistanceTime = now - beTime;

   NSString* distanceStr;

   NSDate* beDate = [NSDatedateWithTimeIntervalSince1970:beTime];

   NSDateFormatter* df = [[NSDateFormatteralloc]init];

    [dfsetDateFormat:@"HH:mm"];

   NSString* timeStr = [dfstringFromDate:beDate];

    [dfsetDateFormat:@"dd"];

   NSString* nowDay = [dfstringFromDate:[NSDatedate]];

   NSString* lastDay = [dfstringFromDate:beDate];

   if(distanceTime <60) {//小于一分钟

        distanceStr =@"刚刚";

    }

   elseif(distanceTime <60*60) {//时间小于一个小时

        distanceStr = [NSStringstringWithFormat:@"%ld分钟前",(long)distanceTime/60];

    }

   elseif(distanceTime <24*60*60&& [nowDayintegerValue] == [lastDayintegerValue]){//时间小于一天

        distanceStr = [NSStringstringWithFormat:@"今天 %@",timeStr];

    }

   elseif(distanceTime<24*60*60*2&& [nowDayintegerValue] != [lastDayintegerValue]){

       if([nowDayintegerValue] - [lastDayintegerValue] ==1|| ([lastDayintegerValue] - [nowDayintegerValue] >10&& [nowDayintegerValue] ==1)) {

            distanceStr = [NSStringstringWithFormat:@"昨天 %@",timeStr];

        }

       else{

            [dfsetDateFormat:@"MM-dd HH:mm"];

            distanceStr = [dfstringFromDate:beDate];

        }

    }

   elseif(distanceTime <24*60*60*365){

        [dfsetDateFormat:@"MM-dd HH:mm"];

        distanceStr = [dfstringFromDate:beDate];

    }

   else{

        [dfsetDateFormat:@"yyyy-MM-dd HH:mm"];

        distanceStr = [dfstringFromDate:beDate];

    }

   returndistanceStr;

}

相关文章

网友评论

      本文标题:iOS 关于评论时间的判断

      本文链接:https://www.haomeiwen.com/subject/pfvvvrtx.html