js 刚刚、几分钟前、几天前、几月前、几年前

 beautifyTime = time => {
    // time为时间戳
    const delta = (new Date().getTime() - time) / 1000
    if (delta / (60 * 60 * 24 * 365) > 1)
      return `${parseInt(delta / (60 * 60 * 24 * 365))}年前`
    if (delta / (60 * 60 * 24 * 30) > 1)
      return `${parseInt(delta / (60 * 60 * 24 * 30))}个月前`
    if (delta / (60 * 60 * 24 * 7) > 1)
      return `${parseInt(delta / (60 * 60 * 24 * 7))}周前`
    if (delta / (60 * 60 * 24) > 1)
      return `${parseInt(delta / (60 * 60 * 24))}天前`
    if (delta / (60 * 60) > 1) return `${parseInt(delta / (60 * 60))}小时前`
    if (delta / 60 > 1) return `${parseInt(delta / 60)}分钟前`
    return '刚刚'
  }

 

你可能感兴趣的:(JS)