iOS时间字符串处理显示(仿系统短信时间显示)

释放双眼,带上耳机,听听看~!

iOS时间字符串处理显示(仿系统短信时间显示)

/**
 中文格式时间处理
 @param timeStr 传入的时间串 格式:"2019-07-08 10:05"
 @return 返回处理后的时间字符串:周三 16:39
 */
-(NSString *)timeTransformation:(NSString *)timeStr {
    
    NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    
    NSRange containsA = [formatStringForHours rangeOfString:@"a"];
    
    BOOL hasAMPM = containsA.location != NSNotFound;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    
    NSArray *dateArr = [timeStr componentsSeparatedByString:@" "];
    NSArray *preDateArr = [[dateArr firstObject] componentsSeparatedByString:@"-"];
    NSString *preDateStr;
 
    if (preDateArr.count == 3) {
        preDateStr = [NSString stringWithFormat:@"%@年%@月%@日 %@",
                      preDateArr[0],preDateArr[1],preDateArr[2],dateArr.lastObject];
 
    }
    // 聊天时间
    NSDate *create = [formatter dateFromString:timeStr];
    
    if (create.isThisYear) { // 今年
        
        if (create.isWithinSevendays) { //是否处于七天内
            NSLog(@"七天内");
            
            if (create.isToday) { // 今天
                
                if (hasAMPM) {   //12小时制
                    NSDateComponents *cmps = [create currentTime];
                    if (cmps.hour<=12){
                        
                        formatter.AMSymbol = @"上午";
                        if (cmps.hour<10) {
                            formatter.dateFormat = @"aaah:mm";
                        }else{
                            formatter.dateFormat = @"aaahh:mm";
                        }
                    }else {
                        formatter.PMSymbol = @"下午";
                        if (cmps.hour%12 <10) {
                            formatter.dateFormat = @"aaah:mm";
                        }else{
                            formatter.dateFormat = @"aaahh:mm";
                        }
                    }
                    //                 return [formatter stringFromDate:create];
                    return [NSString stringWithFormat:@"%@ %@",@"今天",[formatter stringFromDate:create]];
                    
                }else {
                    
                    formatter.dateFormat = @"HH:mm";
                    
                    return  [NSString stringWithFormat:@"%@ %@",@"今天",[formatter stringFromDate:create]];//[formatter stringFromDate:create];
                }
                
                
            } else if (create.isYesterday) { // 昨天
                
                if (hasAMPM) {   //12小时制
                    NSDateComponents *cmps = [create currentTime];
                    if (cmps.hour<=12){
                        
                        formatter.AMSymbol = @"昨天 上午";
                        if (cmps.hour<10) {
                            formatter.dateFormat = @"aaah:mm";
                        }else{
                            formatter.dateFormat = @"aaahh:mm";
                        }
                    }else {
                        formatter.PMSymbol = @"昨天 下午";
                        if (cmps.hour%12 <10) {
                            formatter.dateFormat = @"aaah:mm";
                        }else{
                            formatter.dateFormat = @"aaahh:mm";
                        }
                    }
                    return [formatter stringFromDate:create];
                    
                }else {
                    
                    
                    formatter.dateFormat = @"HH:mm";
                    return  [NSString stringWithFormat:@"%@ %@",@"昨天",[formatter stringFromDate:create]];
                }
                
            }else if (create.iSThebeforeday) { // 前天
                
                if (hasAMPM) {   //12小时制
                    NSDateComponents *cmps = [create currentTime];
                    if (cmps.hour<=12){
                        
                        formatter.AMSymbol = @"前天 上午";
                        if (cmps.hour<10) {
                            formatter.dateFormat = @"aaah:mm";
                        }else{
                            formatter.dateFormat = @"aaahh:mm";
                        }
                    }else {
                        formatter.PMSymbol = @"前天 下午";
                        if (cmps.hour%12 <10) {
                            formatter.dateFormat = @"aaah:mm";
                        }else{
                            formatter.dateFormat = @"aaahh:mm";
                        }
                    }
                    return [formatter stringFromDate:create];
                    
                }else {
                    
                    
                    formatter.dateFormat = @"HH:mm";
                    return  [NSString stringWithFormat:@"%@ %@",@"前天",[formatter stringFromDate:create]];
                }
                
            } else if (create.isThisWeak){ //本星期
                
                
                if (hasAMPM) {   //12小时制
                    NSDateComponents *cmps = [create currentTime];
                    
                    formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
                    
                    if (cmps.hour<=12){
                        
                        formatter.AMSymbol = @"上午";
                        if (cmps.hour<10) {
                            formatter.dateFormat = @"EEEE aaah:mm";
                        }else{
                            formatter.dateFormat = @"EEEE aaahh:mm";
                        }
                    }else {
                        formatter.PMSymbol = @"下午";
                        if (cmps.hour%12 <10) {
                            formatter.dateFormat = @"EEEE aaah:mm";
                        }else{
                            formatter.dateFormat = @"EEEE aaahh:mm";
                        }
                    }
                    
                    return [formatter stringFromDate:create];
                    
                }else {
                    
                    formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
                    
                    formatter.dateFormat = @"EEEE HH:mm";
                    
                    return [formatter stringFromDate:create];
                }
            }else {
                
                if (hasAMPM) {   //12小时制
                    NSDateComponents *cmps = [create currentTime];
                    
                    formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
                    
                    if (cmps.hour<=12){
                        
                        formatter.AMSymbol = @"上午";
                        if (cmps.hour<10) {
                            formatter.dateFormat = @"EEEE aaah:mm";
                        }else{
                            formatter.dateFormat = @"EEEE aaahh:mm";
                        }
                    }else {
                        formatter.PMSymbol = @"下午";
                        if (cmps.hour%12 <10) {
                            formatter.dateFormat = @"EEEE aaah:mm";
                        }else{
                            formatter.dateFormat = @"EEEE aaahh:mm";
                        }
                    }
                    
                    return [formatter stringFromDate:create];
                    
                }else {
                    
                    formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
                    
                    formatter.dateFormat = @"EEEE HH:mm";
                    
                    return [formatter stringFromDate:create];
                }
                
            }
        }
        else {  //今年其他日期
            
            preDateStr = [NSString stringWithFormat:@"%@月%@日",
                          preDateArr[1],preDateArr[2]];
            
            formatter.weekdaySymbols = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
    
            if (hasAMPM) {   //12小时制
                NSDateComponents *cmps = [create currentTime];
                 if (cmps.hour<=12){
                    
                    formatter.AMSymbol = @"上午";
                    if (cmps.hour<10) {
                        formatter.dateFormat = @"EEEE aaah:mm";
                    }else{
                        formatter.dateFormat = @"EEEE aaahh:mm";
                    }
                }else {
                    formatter.PMSymbol = @"下午";
                    if (cmps.hour%12 <10) {
                        formatter.dateFormat = @"EEEE aaah:mm";
                    }else{
                        formatter.dateFormat = @"EEEE aaahh:mm";
                    }
                }
                
            }else {
                formatter.dateFormat = @"EEEE HH:mm";
            }
//            return [formatter stringFromDate:create];
            
            return [NSString stringWithFormat:@"%@ %@",preDateStr,[formatter stringFromDate:create]];
        }
    } else { // 非今年
        
        return preDateStr;
    }
    
}

 

为TA充电
共{{data.count}}人
人已赞赏
iOS文章

IOS Base64编码/解码

2021-4-21 19:39:24

iOS文章

iOS开发时间戳(或date)转字符串

2021-4-21 20:54:29

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索