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];
}