// // NIMTimeUtils.m // YUMI // // Created by zu on 2021/11/26. // #import "NIMTimeUtils.h" @implementation NIMTimeUtils + (NSString *)formattedTimeFromInterval:(NSTimeInterval)timeInterval { // 获取当前日历 NSCalendar *calendar = [NSCalendar currentCalendar]; // 获取当前日期 NSDate *currentDate = [NSDate date]; NSDate *inputDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; // 检查输入日期是否是今天 if ([calendar isDate:inputDate inSameDayAsDate:currentDate]) { // 如果是今天,返回 hour:min 格式 NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; timeFormatter.dateFormat = @"HH:mm"; // 小时和分钟格式 return [timeFormatter stringFromDate:inputDate]; } else { // 如果不是今天,返回 year/month/day 格式 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy/MM/dd"; // 年/月/日格式 return [dateFormatter stringFromDate:inputDate]; } } + (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",YMLocalizedString(@"NIMTimeUtils1"),result,hour,(int)msgDateComponents.minute] : YMLocalizedString(@"NIMTimeUtils1"); } else if(isSameMonth && (nowDateComponents.day == (msgDateComponents.day+2))) //前天 { result = showDetail? [[NSString alloc] initWithFormat:@"%@%@ %zd:%02d",YMLocalizedString(@"NIMTimeUtils3"),result,hour,(int)msgDateComponents.minute] : YMLocalizedString(@"NIMTimeUtils3"); } 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 = YMLocalizedString(@"NIMTimeUtils4"); } else if (totalMin > 5 * 60 && totalMin < 12 * 60) { showPeriodOfTime = YMLocalizedString(@"NIMTimeUtils5"); } else if (totalMin >= 12 * 60 && totalMin <= 18 * 60) { showPeriodOfTime = YMLocalizedString(@"NIMTimeUtils6"); } else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) || totalMin == 0) { showPeriodOfTime = YMLocalizedString(@"NIMTimeUtils7"); } return showPeriodOfTime; } +(NSString*)weekdayStr:(NSInteger)dayOfWeek { static NSDictionary *daysOfWeekDict = nil; daysOfWeekDict = @{@(1):YMLocalizedString(@"NIMTimeUtils8"), @(2):YMLocalizedString(@"NIMTimeUtils9"), @(3):YMLocalizedString(@"NIMTimeUtils10"), @(4):YMLocalizedString(@"NIMTimeUtils11"), @(5):YMLocalizedString(@"NIMTimeUtils12"), @(6):YMLocalizedString(@"NIMTimeUtils13"), @(7):YMLocalizedString(@"NIMTimeUtils14"),}; return [daysOfWeekDict objectForKey:@(dayOfWeek)]; } @end