element 中<el-date-picker></el-date-picker>限制时间为只能选7天内的日期用法全解

HTML部分

 <el-form-item
   label="财务审时间:"
    class="item"
    prop="financialAuditTime"
    
  >
    <el-date-picker
      size="small"
      v-model="queryParams.financialAuditTime"
      type="datetimerange"
      format="yyyy-MM-dd HH:mm:ss"
      value-format="yyyy-MM-dd HH:mm:ss"
      :picker-options="expireTimeOption"
      :default-time="default_time"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      @change="set_default_time"
    >
    </el-date-picker>
  </el-form-item>

return下

 default_time: ["00:00:00"],
      timeOptionRange: '',
      expireTimeOption: {
        onPick: time => {
          // 选择开始时间未选择结束时间
          if (time.minDate && !time.maxDate) {
            this.timeOptionRange = time.minDate;
          }
          if (time.maxDate) {
            this.timeOptionRange = null;
          }
        },
        disabledDate: time => {
          let timeOptionRange = this.timeOptionRange;

          let secondNum = 1000 * 60 * 60 * 24 * 6;
          let times = new Date(new Date(new Date().toLocaleDateString()).getTime() +24 * 60 * 60 * 1000 -1);
          if (!timeOptionRange) {
            return time.getTime() > times
          
          }
          if (( timeOptionRange.getTime() + secondNum) >times) {
            return time.getTime() >times || time.getTime() < timeOptionRange.getTime() - secondNum;
          }
          return time.getTime() < timeOptionRange.getTime()  || time.getTime() > timeOptionRange.getTime() + secondNum;
        }    
      },

methods下

set_default_time(val) {
      let stamp = new Date().getTime() + 1000 * 5 * 60;
      let Hh = formatHh(stamp);//formatHh为封装好的格式,用时引入//import { formatHh } from "utils/formatDate.js";
      this.$set(this.default_time, [0], Hh);
    },

utils/formatDate.js 封装好的日期格式化

/*
 * formatDate(1488161069734, 'yyyy-MM-dd hh:mm:ss')
 * "17-02-27 10:04:29"
 */
import getNewDate from "utils/getNewDate.js";
export default function formatDate(value, format) {
	//let curDate = new Date(value);
	let curDate = new Date(value);
	const o = {
		'M+': curDate.getMonth() + 1, // 月份
		'd+': curDate.getDate(), // 日
		'h+': curDate.getHours(), // 小时
		'm+': curDate.getMinutes(), // 分
		's+': curDate.getSeconds(), // 秒
		'q+': Math.floor((curDate.getMonth() + 3) / 3), // 季度
		'S': curDate.getMilliseconds() // 毫秒
	};
	if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (curDate.getFullYear() + '').substr(4 - RegExp.$1.length));
	for (let k in o) {
		if (new RegExp('(' + k + ')').test(format)) format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
	}
	return format;
};
//时间格式化(HH:mm:ss)
export function formatHh(timestamp) {
	var date = new Date(timestamp);
	var year = date.getFullYear();
	var month = addZero(date.getMonth() + 1);
	var day = addZero(date.getDate());
	var hours = addZero(date.getHours());
	var minutes = addZero(date.getMinutes());
	var seconds = addZero(date.getSeconds());
	return hours + ':' + minutes + ':' + seconds;
	// return year + '-' + month + '-' + day;
}
function addZero(num) {
	return num < 10 ? '0' + num : num;
}

看一下效果

element 中<el-date-picker></el-date-picker>限制时间为只能选7天内的日期用法全解

上一篇:MYSQL查询今天、昨天、7天前、30天、本月数据


下一篇:js动态获取实时时间