时间戳

方法一后台给的是yyyy-MM-dd HH:mm:ss.SSS

+ (NSString *)timeInfoWithDateString:(NSString *)dateString {  
  // 把日期字符串格式化为日期对象  
  NSDate *date = [NSDate dateFromString:dateString withFormat:@"yyyy-MM-dd HH:mm:ss"];  
    
  NSDate *curDate = [NSDate date];  
  NSTimeInterval time = -[date timeIntervalSinceDate:curDate];  
    
  int month = (int)([curDate getMonth] - [date getMonth]);  
  int year = (int)([curDate getYear] - [date getYear]);  
  int day = (int)([curDate getDay] - [date getDay]);  
    
  NSTimeInterval retTime = 1.0;  
  // 小于一小时  
  if (time < 3600) {  
    retTime = time / 60;  
    retTime = retTime <= 0.0 ? 1.0 : retTime;  
    return [NSString stringWithFormat:@"%.0f分钟前", retTime];  
  }  
  // 小于一天,也就是今天  
  else if (time < 33600 * 24) {  
    retTime = time / 3600;  
    retTime = retTime <= 0.0 ? 1.0 : retTime;  
    return [NSString stringWithFormat:@"%.0f小时前", retTime];  
  }  
  // 昨天  
  else if (time < 33600 * 224 * 2) {  
    return @"昨天";  
  }  
  // 第一个条件是同年,且相隔时间在一个月内  
  // 第二个条件是隔年,对于隔年,只能是去年12月与今年1月这种情况  
  else if ((abs(year) == 0 && abs(month) <= 1)  
           || (abs(year) == 1 && [curDate getMonth] == 1 && [date getMonth] == 12)) {  
    int retDay = 0;  
    // 同年  
    if (year == 0) {  
      // 同月  
      if (month == 0) {  
        retDay = day;  
      }  
    }  
      
    if (retDay <= 0) {  
      // 这里按月最大值来计算  
      // 获取发布日期中,该月总共有多少天  
      int totalDays = [NSDate daysInMonth:(int)[date getMonth] year:(int)[date getYear]];  
      // 当前天数 + (发布日期月中的总天数-发布日期月中发布日,即等于距离今天的天数)  
      retDay = (int)[curDate getDay] + (totalDays - (int)[date getDay]);  
        
      if (retDay >= totalDays) {  
        return [NSString stringWithFormat:@"%d个月前", (abs)(MAX(retDay / 31, 1))];  
      }  
    }  
      
    return [NSString stringWithFormat:@"%d天前", (abs)(retDay)];  
  } else  {  
    if (abs(year) <= 1) {  
      if (year == 0) { // 同年  
        return [NSString stringWithFormat:@"%d个月前", abs(month)];  
      }  
        
      // 相差一年  
      int month = (int)[curDate getMonth];  
      int preMonth = (int)[date getMonth];  
        
      // 隔年,但同月,就作为满一年来计算  
      if (month == 12 && preMonth == 12) {  
        return @"1年前";  
      }  
        
      // 也不看,但非同月  
      return [NSString stringWithFormat:@"%d个月前", (abs)(12 - preMonth + month)];  
    }  
      
    return [NSString stringWithFormat:@"%d年前", abs(year)];  
  }  
    
  return @"1小时前";  
}  

方式二 后台给的格式为 纯数字23413423423

/** 通过行数, 返回更新时间 */
- (NSString *)updateTimeForRow:(NSInteger)row {
    // 获取当前时时间戳
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    // 创建歌曲时间戳
     NSTimeInterval createTime = self.model.tracks.list[row].createdAt/1000;
    // 时间差
    NSTimeInterval time = currentTime - createTime;

    // 秒转小时
    NSInteger hours = time/3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld小时前",hours];
    }
    //秒转天数
    NSInteger days = time/3600/24;
    if (days < 30) {
        return [NSString stringWithFormat:@"%ld天前",days];
    }
    //秒转月
    NSInteger months = time/3600/24/30;
    if (months < 12) {
        return [NSString stringWithFormat:@"%ld月前",months];
    }
    //秒转年
    NSInteger years = time/3600/24/30/12;
    return [NSString stringWithFormat:@"%ld年前",years];
}

你可能感兴趣的:(时间戳)