iOS类似微信聊天界面时间转换
#import <Foundation/Foundation.h>
//聊天时间转换工具类
@interface GetChatTime : NSObject
/**
聊天时间转换器,非常好用
@param TimeInterval 时间,传入毫秒,里面自动转为秒(double类型)
@param needTime 是否显示昨天今天后面的时间
@return 时间字符串
*/
+(NSString *)getMessageDateStringFromTimeInterval:(NSTimeInterval)TimeInterval andNeedTime:(BOOL)needTime;
@end
#import "GetChatTime.h"
@implementation GetChatTime
/**
聊天时间转换器,非常好用(调用此方法)
@param TimeInterval 时间,传入毫秒,里面自动转为秒(double类型)
@param needTime 是否显示昨天今天后面的时间
@return 时间字符串
*/
+(NSString *)getMessageDateStringFromTimeInterval:(NSTimeInterval)TimeInterval andNeedTime:(BOOL)needTime
{
//毫秒转秒(必须,否则死循环)
TimeInterval= TimeInterval/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:TimeInterval];
return [GetChatTime getMessageDateString:date andNeedTime:needTime];
}
+ (NSString*)getMessageDateString:(NSDate*)messageDate andNeedTime:(BOOL)needTime
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale systemLocale]];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:@"YYYY/MM/dd HH:mm"];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:messageDate];
NSDate *msgDate = [cal dateFromComponents:components];
NSString*weekday = [GetChatTime getWeekdayWithNumber:components.weekday];
components = [cal components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
if([today isEqualToDate:msgDate]){
if (needTime) {
[formatter setDateFormat:@"今天 HH:mm"];
}
else{
[formatter setDateFormat:@"今天"];
}
return [formatter stringFromDate:messageDate];
}
components.day -= 1;
NSDate *yestoday = [cal dateFromComponents:components];
if([yestoday isEqualToDate:msgDate]){
if (needTime) {
[formatter setDateFormat:@"昨天 HH:mm"];
}
else{
[formatter setDateFormat:@"昨天"];
}
return [formatter stringFromDate:messageDate];
}
#pragma mark ========= 修改代码为 i <= 5,修改前 i <= 6=============
for (int i = 1; i <= 5; i++) {
components.day -= 1;
NSDate *nowdate = [cal dateFromComponents:components];
if([nowdate isEqualToDate:msgDate]){
if (needTime) {
[formatter setDateFormat:[NSString stringWithFormat:@"%@ HH:mm",weekday]];
}
else{
[formatter setDateFormat:[NSString stringWithFormat:@"%@",weekday]];
}
return [formatter stringFromDate:messageDate];
}
}
while (1) {
components.day -= 1;
NSDate *nowdate = [cal dateFromComponents:components];
if ([nowdate isEqualToDate:msgDate]) {
if (!needTime) {
[formatter setDateFormat:@"YYYY/MM/dd"];
}
return [formatter stringFromDate:messageDate];
break;
}
}
}
//1代表星期日、如此类推
+(NSString *)getWeekdayWithNumber:(int)number
{
switch (number) {
case 1:
return @"星期日";
break;
case 2:
return @"星期一";
break;
case 3:
return @"星期二";
break;
case 4:
return @"星期三";
break;
case 5:
return @"星期四";
break;
case 6:
return @"星期五";
break;
case 7:
return @"星期六";
break;
default:
return @"";
break;
}
}
@end
下面这个大家不用看,仅供参考,类似微信聊天界面每隔5分钟显示一次时间
//计算每条消息是否显示时间
//dataArray:UITableView的数据源
-(void)computeVisibleTimeAndRefreshTable:(NSString *)type{
RCmessageContentModel *modeTime;
long long lastVisibleTime = 0;
for (int i=0; i<self.dataSouce.count; i++) {
modeTime = self.dataSouce[i];
if (i==0) {
//一定显示时间
modeTime.isTimeVisible=true;
//最后的时间
lastVisibleTime = modeTime.sentTime;
}else{
//计算时间差,毫秒(5分钟)
long long timeSpan = modeTime.sentTime - lastVisibleTime;
if (timeSpan>=5*60*1000){
//一定显示时间
modeTime.isTimeVisible=true;
//最后的时间
lastVisibleTime = modeTime.sentTime;
}else{
//不展示时间
modeTime.isTimeVisible=false;
}
}
//时间设置完成后数组替换
self.dataSouce[i]=modeTime;
}
//刷新表格,判断完时间后的刷新
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
if ([type isEqual:@"历史消息"]) {
//tableview滚动到最底部
[UIView scrollTableToFoot:self.tableView Animated:NO];
}else{
//tableview滚动到最底部
[UIView scrollTableToFoot:self.tableView Animated:YES];
}
});
}