iOS时间格式化“刚刚、几分钟前、几小时前”等,[包括时间戳&格式化后的时间]

前言:
iOS中把时间转化成“刚刚、几分钟前、几小时前、几天前、某月某日几点几分、.......”格式
这就需要看后台返回什么样的类型了,在项目中碰到的无非就是,格式化后和时间戳了。所以,这次把这两种我都结合起来,放到这里,直接拿去用!

一、返回格式化后的时间 2016-10-11 12:33:33
pragma mark 时间格式转化

注意:数据返回类型为格式化后的时间 eg: "2016-10-11 12:33:33"

   -(NSString *) compareCurrentTime:(NSString *)str
{
    
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    //得到与当前时间差
    NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
    // timeInterval = timeInterval - 86060;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        
        result = [NSString stringWithFormat:@"刚刚"];
        
    }
    else if((temp = timeInterval/60) <60){
        
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
        
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
    
}

二、返回的是时间戳
pragma mark 时间格式转化

注意:后台返回的时间戳包括10位或者有小数点。
eg:“1480064761” 1480064761.000000

-(NSString *)distanceTimeWithBeforeTime:(double)beTime
{
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - beTime;
    NSString *distanceStr;
    
    NSDate *beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
    NSDateFormatter* df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];
    
    [df setDateFormat:@"dd"];
    NSString *nowDay = [df stringFromDate:[NSDate date]];
    NSString *lastDay = [df stringFromDate:beDate];
    
    if (distanceTime < 60) {
        
        distanceStr = @"刚刚";
        
    }
    else if (distanceTime <60*60) {
        
        distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
        
    }
    else if(distanceTime <246060 && [nowDay integerValue] == [lastDay integerValue]){
        
        distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
        
    }
    else if(distanceTime<246060*2 && [nowDay integerValue] != [lastDay integerValue]){
        
        if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
            distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
        }
        else{
            [df setDateFormat:@"MM-dd HH:mm"];
            distanceStr = [df stringFromDate:beDate];
        }
        
    }
    else if(distanceTime <246060*365){
        
        [df setDateFormat:@"MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
        
    }
    else{
        
        [df setDateFormat:@"yyyy-MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
        
    }
    return distanceStr;
}

三、捎带福利、获取当前时间戳方法,返回10位。
pragma mark 获取当前时间戳

-(NSString *)getCurrentTime{
    NSDate *senddata = [NSDate date];
    NSString *date2 = [NSString stringWithFormat:@"%ld", (long)[senddata timeIntervalSince1970]];
    return date2;
}

你可能感兴趣的:(iOS时间格式化“刚刚、几分钟前、几小时前”等,[包括时间戳&格式化后的时间])