获取近七天近一个月上个月本月的时间

284 阅读1分钟
1:获取进一个月的时间
 // 获取上个月的时间
getTopTime() {
  let nowData = new Date();
  let currentMonth = nowData.getMonth();
  let currentYear = nowData.getFullYear();
  let start = new Date(currentYear, currentMonth - 1, 1);
  let end = new Date(currentYear, currentMonth, 0);
  this.params.startDay = this.formateDate(start)
  this.params.endDay = this.formateDate(end)
},
getThisTime() {
  // 本月的前天数
  let nowData = new Date();
  let currentMonth = nowData.getMonth();
  let currentYear = nowData.getFullYear();
  // 本月的一号
  let start = new Date(currentYear, currentMonth, 1);
  this.params.startDay = this.formateDate(start)
  this.params.endDay = this.formateDate(nowData)
},

2: // 获取近七天和进一个月的数据
getDateRange(dateNow, intervalDays, bolPastTiem) {
  let oneDayTiem = 24 * 60 * 60 * 1000;
  let list = [];
  let lastDay;
  if (bolPastTiem == true) {
    lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTiem)
    list.push(this.formateDate(lastDay))
    list.push(this.formateDate(dateNow))
  } else {
    lastDay = new Date(dateNow.getTime() + intervalDays * oneDayTiem)
    list.push(this.formateDate(lastDay))
    list.push(this.formateDate(dateNow))
  }
  this.params.startDay = list[0]
  this.params.endDay = list[1]
  
},
 3:调用的时候代码:
 const date = new Date()
 this.getDateRange(date, 6, true)

 4:时间转换
formateDate(item) {
  let year = item.getFullYear()
  let month = item.getMonth() + 1
  let day = item.getDate();
  month = month > 0 ? month : '0' + month
  day = day > 0 ? day : '0' + day
  return year + '-' + month + '-' + day
},