js获取当前时间的前后七天组成数组

init() {
    
    
            const timeArr = []
            const timeLineArr = []
            for (let i = 0; i < 15; i++) {
    
    
                const time = new Date(new Date().setDate(new Date().getDate() + i - 7))
                const year = time.getFullYear()
                const month = `0${
      
      time.getMonth() + 1}`.slice(-2)
                const strDate = `0${
      
      time.getDate()}`.slice(-2)
                timeArr.push(`${
      
      year}.${
      
      month}.${
      
      strDate}`)
                timeLineArr.push(`${
      
      year}-${
      
      month}-${
      
      strDate}`)
            }
            console.log('timeArr', timeArr)
            console.log('timeLineArr', timeLineArr)
            this.lineChartObj.xAxisData = timeArr
            this.lineChartObj.tooltipName = timeLineArr
        }

输出结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44845483/article/details/127750027