獲取近一個(gè)月的時(shí)期范圍

代碼示例:


    initDate () {
      var end = new Date()
      var year = end.getFullYear()
      var month = end.getMonth() + 1
      var day = end.getDate()
      var dateObj = {}
      dateObj.end = `${year}-${month}-${day}`
      var endMonthDay = new Date(year, month, 0).getDate() // 當(dāng)前月的總天數(shù)
      if (month - 1 <= 0) { // 如果是1月,年數(shù)往前推一年
        dateObj.start = (year - 1) + '-' + 12 + '-' + (day - 1)
      } else {
        var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate()
        if (startMonthDay < day) { // 1個(gè)月前所在月的總天數(shù)小于現(xiàn)在的天日期
          if (day < endMonthDay) { // 當(dāng)前天日期小于當(dāng)前月總天數(shù)
            dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day) - 1)
          } else {
            dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - 1)
          }
        } else {
          dateObj.start = year + '-' + (month - 1) + '-' + (day - 1)
        }
      }
      this.timeValue = [`${dateObj.start} 00:00:00`, `${dateObj.end} 23:59:59`]
    }

效果:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容