码迷,mamicode.com
首页 > 其他好文 > 详细

网络刷帖时间显示

时间:2016-05-23 19:07:13      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

今年:

  今天:  

    刚刚, 小于一分钟
    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;

}

 

网络刷帖时间显示

标签:

原文地址:http://www.cnblogs.com/PJXWang/p/5520763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!