网络刷帖时间显示

Posted PJXWang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络刷帖时间显示相关的知识,希望对你有一定的参考价值。

今年:

  今天:  

    刚刚, 小于一分钟
    n分钟前, 大于一分钟,小于一小时
    HH:mm:ss 大于一小时,今天之内

  昨天:

    昨天:HH:mm:ss 昨天

  其他:

    MM-dd HH:mm:ss 不是今天和昨天,但在今年内

非今年:

  yyyy-MM-dd HH:mm:ss 不在今年

算法实现

扩展NSDate类:

@interface NSDate (PJXExtension)

-(NSDateComponents *)dateFrom:(NSDate *)from;

-(BOOL)isThisYear;

-(BOOL)isToday;

-(BOOL)isYesterday;

@end

 

#import "NSDate+PJXExtension.h"

@implementation NSDate (PJXExtension)

-(NSDateComponents *)dateFrom:(NSDate *)from

{

    //日历对象

    NSCalendar *calendar = [NSCalendar currentCalendar];

    //比较时间

    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

    NSDateComponents *cmps = [calendar components:unit fromDate:from toDate:self options:kNilOptions];

    return cmps;

}

 

-(BOOL)isThisYear

{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];

    NSInteger selfYear =[calendar component:NSCalendarUnitYear fromDate:self];

    return nowYear == selfYear;

}

-(BOOL)isToday

{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSCalendarUnit   unit =  NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;

    NSDateComponents *nowCmp = [calendar components:unit fromDate:[NSDate date]];

 

     NSDateComponents *selfCmp = [calendar components:unit fromDate:self];

    return  nowCmp.year == selfCmp.year

    &&  nowCmp.month == selfCmp.month

    && nowCmp.day == selfCmp.day;

    

//    //利用日历对象比较是否是今天

//    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];

//    fmt.dateFormat = @"yyyy-MM-dd";

//    NSString *nowStr = [fmt stringFromDate:[NSDate date]];

//    NSString *selfStr = [fmt stringFromDate:self ];

//    return [nowStr isEqualToString:selfStr];

 

}

-(BOOL)isYesterday

{

    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];

    fmt.dateFormat = @"yyyy-MM-dd";

    NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];

    NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self ]];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSCalendarUnit unit = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;

    NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:kNilOptions];

    return cmps.year==0

    && cmps.month == 0

    && cmps.day == 1;

}

@end

 在显示类中实现

-(NSString *)setTime:(NSString *)time

{

    NSLog(@"NSString time:%@",time);

    //获得发帖的时间

    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];

    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    

    NSDate *creat = [fmt dateFromString:time];

    

    //显示时间的设置

    NSString *text = nil;

    if (creat.isThisYear) {

        if(creat.isToday){

            NSDateComponents *cmp = [[NSDate date] dateFrom:creat];

            if (cmp.hour >= 1) {

                text = [NSString stringWithFormat:@"%zd小时前",cmp.hour];

            }else if(cmp.minute >= 1){

                text = [NSString stringWithFormat:@"%zd分钟前",cmp.minute];

            }else{

                text = @"刚刚";

            }

        }else if(creat.isYesterday){

            fmt.dateFormat = @"昨天: HH:mm:ss";

            text = [fmt stringFromDate:creat];

        }else{

            fmt.dateFormat = @"MM-dd HH:mm:ss";

            text = [fmt stringFromDate:creat];

 

        }

    }else{

         text = time;

    }

    NSLog(@"Display time:%@",text);

    return text;

}

 

以上是关于网络刷帖时间显示的主要内容,如果未能解决你的问题,请参考以下文章

开挂!!比水军还恶心的刷帖,用Python实现自动评论盖楼抽大奖

xDeepFM

时变网络置乱算法实现

JAVA实现BP神经网络算法

干货时间 | 用JS实现简单的神经网络算法

JAVA实现BP神经网络算法