使用说明,在这里是定义一个类继承于NSObject即可,调用下面的方法即可
#import "CIOTimer.h"
@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
具体的使用方法如下
1.导入类名 #import "CIOTimer.h"
2. 导入后台返回的时间戳
NSDate *datedate = [NSDate dateWithTimeIntervalSince1970:[@"1363948516" floatValue]];
NSString *string1 = [CIOTimer compareCurrentTime: datedate];
NSLog(@"返回=%@",string1);
具体的代码 密码: ycid
补充:时间戳转具体的时间
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[@"1363948516" floatValue]];
- (NSString *)getBabyDetailAge:(NSDate *)date
{
// NSTimeInterval time=[date doubleValue];
// NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//NSLog(@"date:%@",[detaildate description]);
NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];
[formatter_ setDateFormat:@"MM-dd HH:mm"];
NSString *dateString = [formatter_ stringFromDate:date];
dateString = [dateString stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
return dateString;
}