js 当天零点到当前时间的数组

getNowDate() {
      let date = new Date()
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      let hour = date.getHours()
      let minute = date.getMinutes()
      let second = date.getSeconds()
      return [
        year + '-' + this.addZero(month) + '-' + this.addZero(day) + ' 00:00:00',
        year + '-' + this.addZero(month) + '-' + this.addZero(day) + ' ' + this.addZero(hour) + ':' + this.addZero(minute) + ':' + this.addZero(second)
      ]
    },
    addZero(val) {
      return val < 10 ? ('0' + val) : val
    },
    GetDateDiff(startTime, endTime, diffType) {
      //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 
      startTime = startTime.replace(/\-/g, "/");
      endTime = endTime.replace(/\-/g, "/");
      //将计算间隔类性字符转换为小写
      diffType = diffType.toLowerCase();
      var sTime = new Date(startTime); //开始时间
      var eTime = new Date(endTime); //结束时间
      //作为除数的数字
      var timeType = 1;
      switch (diffType) {
        case "second":
          timeType = 1000;
          break;
        case "minute":
          timeType = 1000 * 60;
          break;
        case "hour":
          timeType = 1000 * 3600;
          break;
        case "day":
          timeType = 1000 * 3600 * 24;
          break;
        default:
          break;
      }
      return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
    }
let hourarr = this.getNowDate();
    let daysnum = this.GetDateDiff(this.getNowDate()[0],this.getNowDate()[1],'hour'); //10
    let numscc = [...new Array(daysnum)].map((i, idx) => this.$moment().startOf("hour").subtract(idx, "hours").format("hh:mm")); //
    console.log('hourarr',hourarr)
    console.log('daysnum',daysnum)
    console.log('numscc',numscc)

 

posted @ 2023-03-23 10:31  abcByme  阅读(70)  评论(0编辑  收藏  举报