NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:_dataArray.publishertime];
NSTimeInterval late = [date timeIntervalSince1970]*1;
NSString *timeString = nil;
NSDate *dates = [NSDate date];
NSTimeInterval now = [dates timeIntervalSince1970]*1;
NSTimeInterval cha = now - late;
if (cha/3600<1) { //一小时3600秒
timeString = [NSString stringWithFormat:@"%f",cha/60];
timeString = [timeString substringToIndex:timeString.length-7]; //外国和中国时差7小时
timeString = [NSString stringWithFormat:@"%@分钟前",timeString];
_releaseTimeLabel.text = [NSString stringWithFormat:@"%@",timeString];
}
if (cha/3600 >1 && cha/86400 <1) {
timeString = [NSString stringWithFormat:@"%f",cha/3600];
timeString = [timeString substringToIndex:timeString.length-7];
timeString = [NSString stringWithFormat:@"%@小时前",timeString];
_releaseTimeLabel.text = [NSString stringWithFormat:@"%@",timeString];
}
if (cha/86400 >1) {
timeString = [NSString stringWithFormat:@"%f",cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
int num = [timeString intValue];
if (num <2) {
timeString = [NSString stringWithFormat:@"昨天"];
_releaseTimeLabel.text = [NSString stringWithFormat:@"%@",timeString];
}else if (num ==2 ){
timeString = [NSString stringWithFormat:@"前天"];
_releaseTimeLabel.text = [NSString stringWithFormat:@"%@",timeString];
}else if (num >2 ){
timeString = [NSString stringWithFormat:@"%@天前",timeString];
_releaseTimeLabel.text = [NSString stringWithFormat:@"%@",timeString];
}
}