获取近15天日期

data(){
 return{
  dateList:[]
 }
}
mounted(){
 this.chart2(new Date())
},
methods:{
// 获取当前时间,day为number,getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;【以此类推】
    getDay(day) {
      var today = new Date()
      var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
      today.setTime(targetday_milliseconds) // 注意,这行是关键代码
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return `${tYear}/${tMonth}/${tDate}`
    },
    // 获取近15天日期方法
    chart2(datas) {
      var xdata = []
      for (var i = 0; i < 15; i++) {
        xdata[i] = this.getDay(i)
      }
      this.dateList = xdata
      console.log('this.dateList', this.dateList)
    },
    doHandleMonth(month) {
      var m = month
      if (month.toString().length == 1) {
        m = `0${month}`
      }
      return m
    }
}

你可能感兴趣的:(获取近15天日期)