7.时间戳与实践的转化(CIOTimer)

1.代码的使用

labeldate.text = [CIOTimer compareCurrentTime:[NSDate dateWithTimeIntervalSince1970: [string doubleValue]]];

7.时间戳与实践的转化(CIOTimer)_第1张图片
NSDate类型就好
2.CIOTimer.h里面的代码
#import 

@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

你可能感兴趣的:(7.时间戳与实践的转化(CIOTimer))