项目中有聊天功能,老板让聊天的时间转换成和 QQ 的聊天时间一样的格式,
研究了一下,来分析下 QQ 的聊天格式时间
代码:
会话列表显示的时间 如需转成24小时制,需要使用 HH
//时间显示内容 -(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 *)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.AMSymbol = @"上午"; dateFmt.PMSymbol = @"下午"; dateFmt.dateFormat = @"昨天 aaahh:mm"; } else { if ((nowCmps.day-myCmps.day) <=7) { dateFmt.AMSymbol = @"上午"; dateFmt.PMSymbol = @"下午"; switch (comp.weekday) { case 1: dateFmt.dateFormat = @"星期日 aaahh:mm"; break; case 2: dateFmt.dateFormat = @"星期一 aaahh:mm"; break; case 3: dateFmt.dateFormat = @"星期二 aaahh:mm"; break; case 4: dateFmt.dateFormat = @"星期三 aaahh:mm"; break; case 5: dateFmt.dateFormat = @"星期四 aaahh:mm"; break; case 6: dateFmt.dateFormat = @"星期五 aaahh:mm"; break; case 7: dateFmt.dateFormat = @"星期六 aaahh:mm"; break; default: break; } }else { dateFmt.dateFormat = @"MM-dd hh:mm"; } } } return [dateFmt stringFromDate:myDate]; }