美文网首页iOS Developer
iOS-OC-时间戳距离现在的时间计算

iOS-OC-时间戳距离现在的时间计算

作者: 千年积木 | 来源:发表于2017-07-27 09:27 被阅读79次
    #pragma mark- 时间戳计算
    + (NSString *)LabelFinallyTime:(NSString *)yetTime{
        
        NSDate * nowDate = [NSDate date];
        
        NSTimeInterval now = [nowDate timeIntervalSince1970];
        NSTimeInterval yet = [yetTime doubleValue];
        
        //    NSLog(@"yet = %.f",yet);
        //    NSLog(@"now = %.f",now);
        
        
        NSTimeInterval newTime = now - yet;
        //    NSLog(@"new = %.f",newTime);
        
        NSString * mm = [NSString stringWithFormat:@"%.2f",newTime/60];
        NSString * hh = [NSString stringWithFormat:@"%.2f",newTime/60/60];
        NSString * dd = [NSString stringWithFormat:@"%.2f",newTime/60/60/24];
        NSString * MM = [NSString stringWithFormat:@"%.2f",newTime/60/60/24/30];
        
        
        //    NSLog(@"mm =%@",mm);
        //    NSLog(@"hh =%@",hh);
        //    NSLog(@"dd =%@",dd);
        //    NSLog(@"MM =%@",MM);
        
        NSString * date;
        
        if ([MM floatValue] >= 1) {
            
            date = [NSString stringWithFormat:@"%.f个月前",[MM floatValue]];
            
        }else if ([dd floatValue] >= 1) {
            
            date = [NSString stringWithFormat:@"%.f天前",[dd floatValue]];
            
        }else if ([hh floatValue] >= 1) {
            
            date = [NSString stringWithFormat:@"%.f小时前",[hh floatValue]];
            
        }else if ([mm floatValue] >= 1) {
            
            date = [NSString stringWithFormat:@"%.f分钟前",[mm floatValue]];
            
        }else {
            
            date = [NSString stringWithFormat:@"%.f秒前",newTime];
        }
        
        //    NSLog(@"%@",date);
        
        return date;
    }
    
    

    相关文章

      网友评论

        本文标题:iOS-OC-时间戳距离现在的时间计算

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