美文网首页iOS开发
iOS - 时间处理集锦

iOS - 时间处理集锦

作者: Gold_xiaohui | 来源:发表于2017-11-24 15:36 被阅读0次

    各位大佬好,最近刚完成了一个项目终于有时间来整理下这些东西了,废话不多说,直接上代码吧。

    时间处理函数

    //1.和时间戳进行比较,转换成(如:3天前)
    + (NSString *)changeTimeShow:(long long)str;
    
    //2.和当前真实时间进行比较
    //参数传入的是固定时间格式(yyyy-MM-dd HH:mm)字符串转换成显示字符串(如:"2017-02-26 24:40" 转换成 "3个月前")
    //str: yyyy-MM-dd HH:mm
    + (NSString *)changeTimeShowWithNowData:(NSString *)str;
    
    //3.获取当前真实时间
    + (NSString *)getNowDate;//@"YYYY-MM-dd hh:mm:ss"
    
    //4.获得当前时间戳
    + (NSString *)GetCurrentTimeString;
    
    //5.时间戳转时间(真实时间)
    + (NSString *)GetTimeIncludeYear:(long long)TimeInteger;
    

    具体代码

    1.和时间戳进行比较

    //1.和时间戳进行比较
    + (NSString *)changeTimeShow:(long long)time{
        //获取当前时间戳
        NSTimeInterval a=[[NSDate date] timeIntervalSince1970]; //*1000; // *1000 是精确到毫秒,不乘就是精确到秒
        NSString *returnStr;
        a = (NSInteger)a;
        NSInteger cNum;
        if (time == a) {
            
            returnStr = @"刚刚";
            
        }else if (time > a){
            
            cNum = time - a;
            if (cNum/(30 *24 *60 *60) > 0) {//一个月
                returnStr = [NSString stringWithFormat:@"%ld月后",cNum/(30 *24 *60 *60)];
            }else{
                if (cNum/(24 *60 *60) > 0) {//一天
                    returnStr = [NSString stringWithFormat:@"%ld天后",cNum/(24 *60 *60)];
                }else{
                    if (cNum/(60 *60) > 0) {
                        returnStr = [NSString stringWithFormat:@"%ld小时后",cNum/(60 *60)];
                    }else{
                        if (cNum/60 > 0) {
                            returnStr = [NSString stringWithFormat:@"%ld分钟后",cNum/(60)];
                        }else{
                            returnStr = [NSString stringWithFormat:@"%ld秒后",cNum];
                        }
                    }
                }
            }
            
        }else if (time < a){
            cNum = a - time;
            if (cNum/(30 *24 *60 *60) > 0) {//一个月
                returnStr = [NSString stringWithFormat:@"%ld月前",cNum/(30 *24 *60 *60)];
            }else{
                if (cNum/(24 *60 *60) > 0) {//一天
                    returnStr = [NSString stringWithFormat:@"%ld天前",cNum/(24 *60 *60)];
                }else{
                    if (cNum/(60 *60) > 0) {
                        returnStr = [NSString stringWithFormat:@"%ld小时前",cNum/(60 *60)];
                    }else{
                        if (cNum/60 > 0) {
                            returnStr = [NSString stringWithFormat:@"%ld分钟前",cNum/(60)];
                        }else{
                            returnStr = [NSString stringWithFormat:@"%ld秒前",cNum];
                        }
                    }
                }
            }
        }
        return returnStr;
    }
    

    2.和当前真实时间进行比较

    //2.和当前真实时间进行比较
    + (NSString *)changeTimeShowWithNowData:(NSString *)str{
        //获取当前时间
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        NSString *DateTime = [formatter stringFromDate:date];
        
        //
    //    NSString *string =@"123456d890";
    //    NSString *str1 = [string substringToIndex:5];//截取掉下标6之前不包括6的字符串
    //    NSLog(@"截取的值为:%@",str1);//12345
    //    NSString *str2 = [string substringFromIndex:3];//截取掉下标4之后包括4的字符串
    //    NSLog(@"截取的值为:%@",str2);//456d890
    //    NSRange range = [string rangeOfString:@"5"];//匹配得到的下标
    //    NSLog(@"rang:%@",NSStringFromRange(range));//4,1
    
        NSString *yearNow = [DateTime substringToIndex:4];
        NSString *monNow = [DateTime substringToIndex:7];
        monNow = [monNow substringFromIndex:5];
        NSString *dayNow = [DateTime substringToIndex:10];
        dayNow = [dayNow substringFromIndex:8];
        NSString *hourNow = [DateTime substringToIndex:13];
        hourNow = [hourNow substringFromIndex:11];
        NSString *minNow = [DateTime substringFromIndex:14];
        minNow = [minNow substringToIndex:2];
        
        NSString *year = [str substringToIndex:4];
        NSString *mon = [str substringToIndex:7];
        mon = [mon substringFromIndex:5];
        NSString *day = [str substringToIndex:10];
        day = [day substringFromIndex:8];
        NSString *hour = [str substringToIndex:13];
        hour = [hour substringFromIndex:11];
        NSString *min = [str substringFromIndex:14];
        min = [min substringToIndex:2];
        
        NSString *StrReturn;
        
        if ([yearNow integerValue] == [year integerValue]) {
            
            if ([monNow integerValue] == [mon integerValue]) {
                
                if ([dayNow integerValue] == [day integerValue]) {
                    
                    if ([hourNow integerValue] == [hour integerValue]) {
                        
                        if ([minNow integerValue] == [min integerValue]) {
                            StrReturn = [NSString stringWithFormat:@"刚刚"];
                        }else if ([minNow integerValue] > [min integerValue]){
                            StrReturn = [NSString stringWithFormat:@"%ld分钟前",[minNow integerValue]-[min integerValue]];
                        }else if ([minNow integerValue] < [min integerValue]){
                            StrReturn = [NSString stringWithFormat:@"%ld分钟后",[min integerValue]-[minNow integerValue]];
                        }
                        
                    }else if ([hourNow integerValue] > [hour integerValue]){
                        StrReturn = [NSString stringWithFormat:@"%ld小时前",[hourNow integerValue]-[hour integerValue]];
                    }else if ([hourNow integerValue] < [hour integerValue]){
                        StrReturn = [NSString stringWithFormat:@"%ld小时后",[hour integerValue]-[hourNow integerValue]];
                    }
                    
                }else if ([dayNow integerValue] > [day integerValue]){
                    StrReturn = [NSString stringWithFormat:@"%ld天前",[dayNow integerValue]-[day integerValue]];
                }else if ([dayNow integerValue] < [day integerValue]){
                    StrReturn = [NSString stringWithFormat:@"%ld天后",[day integerValue]-[dayNow integerValue]];
                }
                
            }else if ([monNow integerValue] > [mon integerValue]){
                StrReturn = [NSString stringWithFormat:@"%ld年前",[monNow integerValue]-[mon integerValue]];
            }else if ([monNow integerValue] < [mon integerValue]){
                StrReturn = [NSString stringWithFormat:@"%ld月后",[mon integerValue]-[monNow integerValue]];
            }
            
        }else if ([yearNow integerValue] > [year integerValue]){
            StrReturn = [NSString stringWithFormat:@"%ld年前",[yearNow integerValue]-[year integerValue]];
        }else if ([yearNow integerValue] < [year integerValue]){
            StrReturn = [NSString stringWithFormat:@"%ld年后",[year integerValue]-[yearNow integerValue]];
        }
        return StrReturn;
    }
    

    3.获取当前真实时间

    //3.获取当前真实时间
    +(NSString *)getNowDate{
        //获取当前时间
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//hh和HH是不一样的HH表示24小时制hh表示12小时
        NSString *DateTime = [formatter stringFromDate:date];
        return DateTime;
    }
    

    4.获得当前时间戳

    //4.获得当前时间戳
    + (NSString *)GetCurrentTimeString{
        
        NSTimeInterval a=[[NSDate date] timeIntervalSince1970]; //*1000; // *1000 是精确到毫秒,不乘就是精确到秒
        NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
        
        return timeString;
    }
    

    5.时间戳转时间(真实时间)

    //5.时间戳转时间(真实时间)
    + (NSString *)GetTimeIncludeYear:(long long)TimeInteger{
        NSLog(@"%ld",TimeInteger);
        long long time = TimeInteger/1000;
        NSLog(@"%ld",time);
        NSDate *TimeDate = [NSDate dateWithTimeIntervalSince1970:time];
        
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        
        //----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        
        NSString *timeString = [formatter stringFromDate:TimeDate];
        
        NSLog(@"timeString-%@",timeString);
        
        return timeString;
    }
    

    相关文章

      网友评论

        本文标题:iOS - 时间处理集锦

        本文链接:https://www.haomeiwen.com/subject/wehfbxtx.html