1.代碼的使用
labeldate.text = [CIOTimer compareCurrentTime:[NSDate dateWithTimeIntervalSince1970: [string doubleValue]]];
NSDate類型就好
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
/**
* 計(jì)算指定時(shí)間與當(dāng)前的時(shí)間差
* @param compareDate 某一指定時(shí)間
* @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