1.代码的使用
labeldate.text = [CIOTimer compareCurrentTime:[NSDate dateWithTimeIntervalSince1970: [string doubleValue]]];
2.CIOTimer.h里面的代码
#import <Foundation/Foundation.h> @interface CIOTimer : NSObject +(NSString *)compareCurrentTime:(NSDate*) compareDate; @end
2.CIOTimer.m里面的代码
#import "CIOTimer.h" @interface CIOTimer () { } @end @implementation CIOTimer /** * 计算指定时间与当前的时间差 * @param compareDate 某一指定时间 * @return 多少(秒or分or天or月or年)+前 (比如,3天前、10分钟前) */ +(NSString *)compareCurrentTime:(NSDate*) compareDate { NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow]; timeInterval = -timeInterval; NSInteger time = round(timeInterval); long temp = 0; if (time < 60) { NSString *result = @"刚刚"; return result; } else if((temp = timeInterval/60) <60){ NSString *result = [NSString stringWithFormat:@"%ld分前",temp]; return result; } else if((temp = temp/60) <24){ NSString *result = [NSString stringWithFormat:@"%ld小前",temp]; return result; } else if((temp = temp/24) <30){ NSString *result = [NSString stringWithFormat:@"%ld天前",temp]; return result; } else if((temp = temp/30) <12){ NSString *result = [NSString stringWithFormat:@"%ld月前",temp]; return result; } else{ temp = temp/12; NSString *result = [NSString stringWithFormat:@"%ld年前",temp]; return result; } return nil; } @end