Click the button to get the last week, this week and next week's time infinitely

Keywords: Vue

Reference blog: https://blog.csdn.net/qq_36742720/article/details/82455061
In the first two days, you need to draw an eckarts chart. Click the buttons of the previous week and the next week to get the data of the chart.
I have been looking for it on the Internet for a long time and found it written by the blogger. It's super. I changed it and wrote it in vue for later use.

// Get last week, this week, and next week's time
    getDates(date) {
      var year = date.getFullYear() + '-'
      var month = ((date.getMonth() + 1) + '-').length === 2 ? ('0' + (date.getMonth() + 1) + '-') : (date.getMonth() + 1) + '-'
      var day = (date.getDate() + '').length === 1 ? ('0' + date.getDate()) : date.getDate()
      this.timeStamp.push(year + month + day)
      return year + month + day
    },
    addDate(date, n) {
      date.setDate(date.getDate() + n)
      return date
    },
    setDate(date) {
      this.timeStamp = []
      var week = date.getDay() - 1
      date = this.addDate(date, week * -1)
      this.currentFirstDate = new Date(date)
      for (var i = 0; i < 7; i++) {
        this.getDates(i === 0 ? date : this.addDate(date, 1)) // Monday starts
      }
    },
    // Click previous week
    lastWeek() {
      this.setDate(this.addDate(this.currentFirstDate, -7))
      this.getGraphChartData(this.timeStamp[0], this.timeStamp[6])
    },
    // This week
    onWeek() {
      this.setDate(new Date())
      this.getGraphChartData(this.timeStamp[0], this.timeStamp[6])
    },
    // Next week
    nextWeek() {
      this.setDate(this.addDate(this.currentFirstDate, 7))
      this.getGraphChartData(this.timeStamp[0], this.timeStamp[6])
    },

The definition written in return:

      // Initial time to click last week next week button
      currentFirstDate: new Date(),
      // Returns a collection of one week's time
      timeStamp: []

In addition, there is a method to calculate the first five minutes and the next few minutes according to a certain time:

        // startTime is the standard time format, ex: 2019-04-23 00:00:00
        var date = new Date(startTime.replace(/-/g, '/'))
        // Standard time to timestamp
        var time = date.getTime()
        // Five minutes before calculation
        var time1 = time - 1000 * 60 * 5
         // 15 minutes after calculation
        var time2 = time + 1000 * 60 * 15
        // Time stamp converted to standard time
        var date1 = this.getTime(time1)
        var date2 = this.getTime(time2)

this.getTime method for standard time:

// Change time format
export function getTime(date) {
  var dateStr = ''
  date = new Date(date)
  var yy = date.getFullYear()
  var mm = parseInt(date.getMonth()) + 1
  var dd = parseInt(date.getDate())
  var hh = date.getHours()
  var min = date.getMinutes()
  var ss = date.getSeconds()
  if (mm < 10) {
    mm = '0' + mm
  }
  if (dd < 10) {
    dd = '0' + dd
  }
  if (hh < 10) {
    hh = '0' + hh
  }
  if (min < 10) {
    min = '0' + min
  }
  if (ss < 10) {
    ss = '0' + ss
  }
  dateStr = yy.toString() + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + ss
  return dateStr
}

Conclusion.

Posted by blacksnday on Sun, 24 Nov 2019 09:45:47 -0800