self.timeLab.text = [NSString stringWithFormat:@"%@",[self compareCurrentTime:model.add_time.formateDateYMD_HM]];
-(NSString *)compareCurrentTime:(NSString *)str
{
//把字符串转为NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate*timeDate = [dateFormatterdateFromString:str];
//得到与当前时间差
NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
timeInterval = -timeInterval;
//标准时间和北京时间差8个小时
// timeInterval = timeInterval - 86060;
longtemp =0;
NSString*result;
if(timeInterval <60) {
result = [NSString stringWithFormat:@"刚刚"];
}
elseif((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%ld分钟前",temp];
}
elseif((temp = temp/60) <24){
result = [NSString stringWithFormat:@"%ld小时前",temp];
}
elseif((temp = temp/24) <30){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
elseif((temp = temp/30) <12){
result = [NSString stringWithFormat:@"%ld月前",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
}
- (NSString *)formateDateYMD_HM {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.doubleValue];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[formatsetDateFormat:@"yyyy-MM-dd HH:mm"];
NSString*result = [formatstringFromDate:date];
returnresult;
}
网友评论