IM聊天会话列表,时间简单判断

51 篇文章 0 订阅
10 篇文章 0 订阅

IM聊天会话列表,时间简单判断

//时间显示内容
-(NSString *)getDateDisplayString:(long long) miliSeconds{
NSLog(@"-时间戳---%lld_----",miliSeconds);

NSTimeInterval tempMilli = miliSeconds;
NSTimeInterval seconds = tempMilli/1000.0;
NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:seconds];

NSCalendar *calendar = [ NSCalendar currentCalendar ];
int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear ;
NSDateComponents *nowCmps = [calendar components:unit fromDate:[ NSDate date ]];
NSDateComponents *myCmps = [calendar components:unit fromDate:myDate];

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

//2. 指定日历对象,要去取日期对象的那些部分.
NSDateComponents *comp =  [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:myDate];

if (nowCmps.year != myCmps.year) {
    dateFmt.dateFormat = @"yyyy-MM-dd hh:mm";
} else {
    if (nowCmps.day==myCmps.day) {
        dateFmt.AMSymbol = @"上午";
        dateFmt.PMSymbol = @"下午";
        dateFmt.dateFormat = @"aaa hh:mm";
        
    } else if((nowCmps.day-myCmps.day)==1) {
        dateFmt.dateFormat = @"昨天";
    } else {
        if ((nowCmps.day-myCmps.day) <=7) {
            switch (comp.weekday) {
                case 1:
                    dateFmt.dateFormat = @"星期日";
                    break;
                case 2:
                    dateFmt.dateFormat = @"星期一";
                    break;
                case 3:
                    dateFmt.dateFormat = @"星期二";
                    break;
                case 4:
                    dateFmt.dateFormat = @"星期三";
                    break;
                case 5:
                    dateFmt.dateFormat = @"星期四";
                    break;
                case 6:
                    dateFmt.dateFormat = @"星期五";
                    break;
                case 7:
                    dateFmt.dateFormat = @"星期六";
                    break;
                default:
                    break;
            }
        }else {
            dateFmt.dateFormat = @"MM-dd hh:mm";
        }
    }
}
return [dateFmt stringFromDate:myDate];
}

评论时间戳的处理:

评论时间的处理

+ (NSString *)mhsq_getDateDisplayString:(long long)seconds {
MHSQ_Log(@"时间戳********%lld********",seconds);

NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:seconds];

NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
NSDateComponents *nowCmps = [calendar components:unit fromDate:[ NSDate date ]];
NSDateComponents *myCmps = [calendar components:unit fromDate:myDate];

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

//2. 指定日历对象,要去取日期对象的那些部分.
//    NSDateComponents *comp =  [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:myDate];

// 不是同一年  2018-10-10 15:10
if (nowCmps.year != myCmps.year) {
    dateFmt.dateFormat = @"yyyy-MM-dd hh:mm";
} else {
    if (nowCmps.day == myCmps.day) {            
        /**
         一个小时内,分同一个小时内:如 16:30:00 与 16:20:00,直接拿当前的 分钟 - 时间戳的分钟  就是10分钟前,
         一个小时内,分不同一个小时内:如 16:30:00、15:20:00, 时:(60 - 时间戳 + 当前的分钟数) / 60   分:(60 - 时间戳 + 当前的分钟数) % 60
         */
        
        if (nowCmps.hour - myCmps.hour == 0) {
            return [NSString stringWithFormat:@"%ld分钟前", nowCmps.minute - myCmps.minute];
        } else if (nowCmps.hour - myCmps.hour == 1 && nowCmps.minute + 60 - myCmps.minute < 60) {
            return [NSString stringWithFormat:@"%ld分钟前", nowCmps.minute + 60 - myCmps.minute];
        } else if (nowCmps.hour - myCmps.hour == 1 && nowCmps.minute + 60 - myCmps.minute >= 60) {
            return [NSString stringWithFormat:@"%ld小时前", nowCmps.hour - myCmps.hour];
        } else if (nowCmps.hour - myCmps.hour > 1) {
            return [NSString stringWithFormat:@"%ld小时前", nowCmps.hour - myCmps.hour];
        }
        
        
    } else if((nowCmps.day - myCmps.day) == 1) {
        // 昨天
        dateFmt.dateFormat = @"昨天 HH:mm";
    } else {

        dateFmt.dateFormat = @"MM-dd HH:mm";
        
    }
}
return [dateFmt stringFromDate:myDate];
}

参考链接: https://www.jianshu.com/p/aa92f82c0998

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值