iOS时间格式简洁展示

作者: 海边的遐想 | 来源:发表于2016-04-14 14:49 被阅读146次

    在这里给大家介绍一种类似微博,微信中看到的时间格式实现方式。

    例如:刚刚 -几分钟前-几小时前等等。

    .h

    #import NSDate

    @interface NSDate (Extension)

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

    -(BOOL)isThisYear;

    -(BOOL)isToday;

    -(BOOL)isYesterday;

    @end

    .m

    #import "NSDate+Extension.h"

    @implementation NSDate (Extension)

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

    //日历

    NSCalendar *calendar = [NSCalendar currentCalendar];

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

    return [calendar components:unit fromDate:from toDate:self options:0 ];

    }

    //判断是否为今年

    -(BOOL)isThisYear{

    NSCalendar * calendar = [NSCalendar currentCalendar];

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

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

    return selfYear == nowYear;

    }

    //判读是否为今天

    -(BOOL)isToday{

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

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

    NSString * nowDate = [formatter stringFromDate:[NSDate date]];

    NSString * selfDate = [formatter stringFromDate:self];

    return [nowDate isEqualToString:selfDate];

    }

    //判断是否为昨天

    -(BOOL)isYesterday{

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

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

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

    NSDate * selfdate = [formatter dateFromString:[formatter stringFromDate:self]];

    NSCalendar * calendar = [NSCalendar currentCalendar];

    NSDateComponents * unit = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:selfdate toDate:nowDate options:0];

    return unit.year == 0 && unit.month == 0 && unit.day == 1;

    }

    @end

    下面的实例所展现的效果:当时间为去年时,显示全部时间格式,当时间为今年时分为以下情况:

    当时间为今天时,判断具体时间产生、几小时前、几分钟前、刚刚的显示格式;

    当时间为昨天是,显示昨天以及时分秒;

    当时间既不是今天又不是昨天时,显示除年以外的所有时间格式

    -(NSString *)create_time{

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

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

    NSDate * creat = [formatter dateFromString:_create_time];

    if (creat.isThisYear) {

    if (creat.isToday) {

    NSDateComponents * unit = [creat dateFrom:creat];

    if (unit.hour > 1) {

    return [NSString stringWithFormat:@"%zd小时前",unit.hour];

    }else if (unit.minute > 1){

    return [NSString stringWithFormat:@"%zd分钟前",unit.minute];

    }else{

    return @"刚刚";

    }

    }else if (creat.isYesterday){

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

    return[formatter stringFromDate:creat];

    }else{

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

    return [formatter stringFromDate:creat];

    }

    }

    else{

    return[formatter stringFromDate:creat];

    }

    }

    相关文章

      网友评论

        本文标题:iOS时间格式简洁展示

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