【js】--獲取開始時間 和 截止時間中間的所有時間


1.工具函數  將【中國標准時間】 轉換成 【年月日 時分秒】

/*
* timeStamp: 標准時間 例: 'Tue Sep 22 2020 00:00:00 GMT+0800 (中國標准時間)'
* fmt: 時間格式
*/
function dealFormData(timeStamp, fmt = 'Y-m-d h:00:00') {
  const date = new Date(timeStamp);
  const tmpYear = date.getFullYear();
  const tmpMon = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1);
  const tmpDay = (date.getDate() > 9) ? date.getDate() : '0' + (date.getDate());
  const tmpHour = (date.getHours()) > 9 ? date.getHours() : '0' + (date.getHours());
  const tmpMinutes = (date.getMinutes()) > 9 ? date.getMinutes() : '0' + (date.getMinutes());
  const tmpSeconds = (date.getSeconds()) > 9 ? date.getSeconds() : '0' + (date.getSeconds());
  let finalData = '';
  switch (fmt) {
    case 'Y-m-d h:m:s':
      finalData = tmpYear + '-' + tmpMon + '-' + tmpDay + ' ' + tmpHour + ':' + tmpMinutes + ':' + tmpSeconds;
      break;
    case 'Y-m-d h:00:00':
      finalData = tmpYear + '-' + tmpMon + '-' + tmpDay + ' ' + tmpHour + ':00:00';
      break;
    case 'Y-m-d':
      finalData = tmpYear + '-' + tmpMon + '-' + tmpDay;
      break;
    default:
      //
  }
  return finalData;
}

2.判斷函數,是否是【中國標准時間】 用到上面的 dealFormDate 方法

 // 判斷是否 中國標准時間
    isChinaStandardTime(time, format) {
      let temp = '';
      // 判斷 時間 是否是 時間字符串, 還是中國標准時間,是中國標准時間 就轉換
      if (time.indexOf('中國標准時間') !== -1) {
        temp = dealFormData(time, format);
        return temp;
      }
      return time;
    },

3.得到中間的時間方法  

    // 得到中間的時間
    /*
    * type: 類型 'hour', 'day'
    * startTime: 開始時間 例:'2020-09-22 00:00:00'
    * endTime: 結束時間   例:'2020-09-23 23:00:00'
    */
    getMiddleData(type, startTime, endTime) {
      const dealTimeArr = [];
      let dealStartTime = new Date(startTime).getTime();
      const dealEndTime = new Date(endTime).getTime();
      if (type === 'hour') {
        while (true) {
          if (dealStartTime <= dealEndTime) {
            const itemTime = this.isChinaStandardTime((new Date(dealStartTime)).toString(), 'Y-m-d h:00:00');
            dealTimeArr.push(itemTime);
            // debugger;
            dealStartTime += 1 * 60 * 60 * 1000;
          } else {
            break;
          }
        }
      } else if (type === 'day') {
        while (true) {
          if (dealStartTime <= dealEndTime) {
            const itemTime = this.isChinaStandardTime((new Date(dealStartTime)).toString(), 'Y-m-d');
            dealTimeArr.push(itemTime);
            // debugger;
            dealStartTime += 24 * 60 * 60 * 1000;
          } else {
            break;
          }
        }
      }
      console.log(dealTimeArr);
    },

4.調用

// 選完 小時數據的結束時間 時 調用接口
    /*
    * t: 中國標准時間,這是 element-ui 時間選擇器的change方法,默認傳進來的值
    */
    queryHourData(t) {
      this.choseTimeHour.startTime = this.isChinaStandardTime(this.choseTimeHour.startTime.toString(), 'Y-m-d h:00:00');
      this.choseTimeHour.endTime = this.isChinaStandardTime(t.toString(), 'Y-m-d h:00:00');
      console.log(this.choseTimeHour);
      console.log('處理時間');
      this.getMiddleData('hour', this.choseTimeHour.startTime, this.choseTimeHour.endTime);
    },

  

element-ui 時間控件  

<el-date-picker style="width:192px;" popper-class="dateRangeStyle"
                  v-model="choseTimeHour.endTime"
                  type="datetime" @change= "queryHourData"
                  placeholder="選擇日期">
                </el-date-picker>

用到的變量:
choseTimeHour: {
    startTime: '',
    endTime: '',
}  

 

 該方法是在使用 Vue框架的時候,用到的,為了便於查看,就寫成了 傳統js的樣式,調用的時候,還是用 vue 的形式調用

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM