获取近一天/近三天/近一周/近一个月/本日/本周/本月/本年的时间

1、近一天 

  let time = new Date().getTime()
  let endTime = time - 1 * 24 * 3600 * 1000
  this.startTime = this.$dayjs(endTime).format('YYYY-MM-DD 00:00:00')
  this.endTime = this.$dayjs(time).format('YYYY-MM-DD 23:59:59')
  console.log(this.startTime) // 开始时间
  console.log(this.endTime) // 结束时间

2、近三天

  let time = new Date().getTime()
  let endTime = time - 3 * 24 * 3600 * 1000
  this.startTime = this.$dayjs(endTime).format('YYYY-MM-DD 00:00:00')
  this.endTime = this.$dayjs(time).format('YYYY-MM-DD 23:59:59')
  console.log(this.startTime) // 开始时间
  console.log(this.endTime) // 结束时间

3、近一周

  // 方法1
  let time = new Date().getTime()
  let endTime = time - 7 * 24 * 3600 * 1000
  this.startTime = this.$dayjs(endTime).format('YYYY-MM-DD 00:00:00')
  this.endTime = this.$dayjs(time).format('YYYY-MM-DD 23:59:59')
  console.log(this.startTime) // 开始时间
  console.log(this.endTime) // 结束时间


 // 方法2
  let time = new Date().getTime()
  let Nowdate = new Date()
  let WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000)
  let WeekLastDay = new Date((WeekFirstDay / 1000 + 6 * 86400) * 1000)
  this.beginTime = this.$dayjs(WeekFirstDay).format('YYYY-MM-DD 00:00:00')
  this.endTime = this.$dayjs(WeekLastDay).format('YYYY-MM-DD 23:59:59')


// 方法3
  let oneDay = 24 * 60 * 1000
  let startTime = new Date(Date.now() - 7 * oneDay)
  this.weekStar = this.$dayjs(startTime).format('YYYY-MM-DD')
  let endTime = new Date(Date.now() - oneDay)
  this.weekEnd = this.$dayjs(endTime).format('YYYY-MM-DD')
  console.log(this.weekStar, '前七天第一天')
  console.log(this.weekEnd, '前七天最后一天')

4、近一个月

  // 方法1
  let time = new Date().getTime()
  let now = new Date() // 当前日期
  let nowMonth = now.getMonth() // 当前月
  let nowYear = now.getFullYear() // 当前年
  let monthStartDate = new Date(nowYear, nowMonth, 1)
  let monthEndDate = new Date(nowYear, nowMonth + 1, 0)
  this.beginTime = this.$dayjs(monthStartDate).format('YYYY-MM-DD 00:00:00')
  this.endTime = this.$dayjs(monthEndDate).format('YYYY-MM-DD 23:59:59')


  // 方法2
  var lw = new Date(myDate - 1000 * 60 * 60 * 24 * 30);//最后一个数字30可改,30天的意思
  var lastY = lw.getFullYear();
  var lastM = lw.getMonth() + 1;
  var lastD = lw.getDate();
  this.monthStart = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD);//三十
  this.monthEnd = nowY + "-" + (nowM < 10 ? "0" + nowM : nowM) + "-" + (nowD < 10 ? "0" + nowD : nowD);//当前日期

  console.log(this.monthStart, '前三十天第一天')
  console.log(this.monthEnd, '前三十天最后一天')

5、本日

let time = new Date().getTime()

this.dayBeginTime = this.$dayjs(time).format('YYYY-MM-DD')
this.dayEndTime = this.$dayjs(time).format('YYYY-MM-DD')
console.log(this.dayBeginTime, '今天开始')
console.log(this.dayEndTime, '今天结束')

6、本周

let Nowdate = new Date()
let WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000)
let WeekLastDay = new Date((WeekFirstDay / 1000 + 6 * 86400) * 1000)
this.weekStar = this.$dayjs(WeekFirstDay).format('YYYY-MM-DD') // 本周第一天
this.weekEnd = this.$dayjs(WeekLastDay).format('YYYY-MM-DD') 

7、本月

let Nowdate = new Date()
let nowMonth = Nowdate.getMonth() // 当前月
let nowYear = Nowdate.getFullYear() // 当前年
let monthStartDate = new Date(nowYear, nowMonth, 1)// 本月的开始时间
let monthEndDate = new Date(nowYear, nowMonth + 1, 0)// 本月的结束时间
this.monthStart = this.$dayjs(monthStartDate).format('YYYY-MM-DD')
this.monthEnd = this.$dayjs(monthEndDate).format('YYYY-MM-DD')

8、本年

let Nowdate = new Date()
let nowYear = Nowdate.getFullYear() // 当前年
let yearStartDate = new Date(nowYear, 0, 1)// 本年的开始时间			
let yearEndDate = new Date(nowYear, 11, 31)// 本年的结束时间
this.quarterStart = this.$dayjs(yearStartDate).format('YYYY-MM-DD')
this.quarterEnd = this.$dayjs(yearEndDate).format('YYYY-MM-DD')
console.log(this.quarterStart, '本年第一天')
console.log(this.quarterEnd, '本年最后一天')

对你有用的话就点个赞叭