Files
yinmeng-ios/xplan-ios/Main/Message/View/NIMTimeUtils.m
2021-12-08 18:53:33 +08:00

97 lines
3.5 KiB
Objective-C

//
// NIMTimeUtils.m
// xplan-ios
//
// Created by zu on 2021/11/26.
//
#import "NIMTimeUtils.h"
@implementation NIMTimeUtils
+ (NSString*)showTime:(NSTimeInterval) msglastTime showDetail:(BOOL)showDetail
{
//今天的时间
NSDate * nowDate = [NSDate date];
NSDate * msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
NSString *result = nil;
NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
NSInteger hour = msgDateComponents.hour;
double OnedayTimeIntervalValue = 24*60*60; //一天的秒数
result = [self getPeriodOfTime:hour withMinute:msgDateComponents.minute];
if (hour > 12)
{
hour = hour - 12;
}
BOOL isSameMonth = (nowDateComponents.year == msgDateComponents.year) && (nowDateComponents.month == msgDateComponents.month);
if(isSameMonth && (nowDateComponents.day == msgDateComponents.day)) //同一天,显示时间
{
result = [[NSString alloc] initWithFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute];
}
else if(isSameMonth && (nowDateComponents.day == (msgDateComponents.day+1)))//昨天
{
result = showDetail? [[NSString alloc] initWithFormat:@"昨天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"昨天";
}
else if(isSameMonth && (nowDateComponents.day == (msgDateComponents.day+2))) //前天
{
result = showDetail? [[NSString alloc] initWithFormat:@"前天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"前天";
}
else if([nowDate timeIntervalSinceDate:msgDate] < 7 * OnedayTimeIntervalValue)//一周内
{
NSString *weekDay = [self weekdayStr:msgDateComponents.weekday];
result = showDetail? [weekDay stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : weekDay;
}
else//显示日期
{
NSString *day = [NSString stringWithFormat:@"%zd-%zd-%zd", msgDateComponents.year, msgDateComponents.month, msgDateComponents.day];
result = showDetail? [day stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute]:day;
}
return result;
}
#pragma mark - Private
+ (NSString *)getPeriodOfTime:(NSInteger)time withMinute:(NSInteger)minute
{
NSInteger totalMin = time *60 + minute;
NSString *showPeriodOfTime = @"";
if (totalMin > 0 && totalMin <= 5 * 60)
{
showPeriodOfTime = @"凌晨";
}
else if (totalMin > 5 * 60 && totalMin < 12 * 60)
{
showPeriodOfTime = @"上午";
}
else if (totalMin >= 12 * 60 && totalMin <= 18 * 60)
{
showPeriodOfTime = @"下午";
}
else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) || totalMin == 0)
{
showPeriodOfTime = @"晚上";
}
return showPeriodOfTime;
}
+(NSString*)weekdayStr:(NSInteger)dayOfWeek
{
static NSDictionary *daysOfWeekDict = nil;
daysOfWeekDict = @{@(1):@"星期日",
@(2):@"星期一",
@(3):@"星期二",
@(4):@"星期三",
@(5):@"星期四",
@(6):@"星期五",
@(7):@"星期六",};
return [daysOfWeekDict objectForKey:@(dayOfWeek)];
}
@end