// 返回发布时间dateWithString - (NSString *)backReleaseTimeWithDateStr:(NSString *)dateWithString{ // 获取当前NSCalendar NSCalendar *calendar = [NSCalendar currentCalendar]; // 设置日期格式 NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; //入参 发布日期 NSDate *releaseTimer = [formatter dateFromString:dateWithString]; if ([calendar isDateInToday:releaseTimer]) { // 今天 //当前时间 NSDate *nowDate = [NSDate date]; // 获得日期之间的间隔 NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; NSDateComponents *components = [calendar components:unit fromDate:releaseTimer toDate:nowDate options:0]; if (components.hour >= 1) { // formatter.dateFormat = @"今天 HH:mm"; // return [formatter stringFromDate:releaseTimer]; return [NSString stringWithFormat:@"%zd小时前",components.hour]; }else if(components.minute >= 1){ return [NSString stringWithFormat:@"%zd分钟前",components.minute]; }else{ return @"刚刚"; } }else if([calendar isDateInYesterday:releaseTimer]){ // 昨天 formatter.dateFormat = @"昨天 HH:mm"; return [formatter stringFromDate:releaseTimer]; }else { // 其他 formatter.dateFormat = @"MM-dd HH:mm"; return [formatter stringFromDate:releaseTimer]; } return dateWithString; }