iOS 计算时间差

/**
 * 计算指定时间与当前的时间差
 * @param compareDate   某一指定时间 
 * @return 多少(秒or分or天or月or年)+前 (比如,3天前、10分钟前) 
 */
+(NSString *) compareCurrentTime:(NSDate*) compareDate
//                         
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSStringstringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
       result = [NSStringstringWithFormat:@"%d分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSStringstringWithFormat:@"%d小前",temp];
    }
     
    else if((temp = temp/24) <30){
        result = [NSStringstringWithFormat:@"%d天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSStringstringWithFormat:@"%d月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSStringstringWithFormat:@"%d年前",temp];
    }
    
    return  result;
}
原文地址:https://www.cnblogs.com/lihaibo-Leao/p/4143754.html