前端時間戳處理去年昨天當天基於dayjs

方法內代碼:參數date爲時間戳

 let that = this;
    if (!date) {
      return;
    }
    date = date * 1000
    const IN_DAY = 1000 * 60 * 60 * 24 * 1; //1天
    const Yesterday = 1000 * 60 * 60 * 24 * 2; //昨天

    let Y = dayjs(date).year(),
      M = dayjs(date).month(),
      D = dayjs(date).date(),
      // W = dayjs(date).day(),
      W = dayjs(date).format('ddd'),
      H = dayjs(date).hour(),
      Min = dayjs(date).format('mm'),
      nowY = dayjs().year(),
      nowM = dayjs().month(),
      nowD = dayjs().date(),
      timeStr = '';

    if (Y < nowY) {
      // 去年
      timeStr = `${y}/${M}/${D} `
    } else {
      if (Y === nowY && M === nowM) {
        // 昨天
        if ((nowD - D) === 1) {
          timeStr = '昨天 '
        } else if (nowD === D) {
          // 當天
          timeStr = ''
        } else {
          // 一週內
          timeStr = W + ' '
        }
      } else {
        timeStr = `${M}/${D} `
      }
    }
    timeStr += `${H}:${Min}`
    return timeStr

 

npm地址:https://www.npmjs.com/package/dayjs

npm安裝:

npm install dayjs --save

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章