element日期時間選擇器限制時間選擇功能實現(xiàn)(精確到小時)
需求:選一個開始時間,要求精確到小時,小于當(dāng)前時刻的小時不讓選擇,只能往后選7天。
如圖:

需要element的DateTimePicker 日期時間選擇器
部分代碼如下:
<el-form-item
label="出發(fā)時間:"
prop="startTime">
<el-date-picker
v-model="editForm.startTime"
:picker-options="pickerOptions"http:// 控制時間選擇
format="yyyy-MM-dd HH:mm"
type="datetime"
placeholder="選擇日期時間"
/>
</el-form-item> pickerOptions: {
disabledDate(time) {
let dateTime = new Date();
let startDateTime = dateTime.setDate(dateTime.getDate() - 1);
let endDateTime = dateTime.setDate(dateTime.getDate() + 7);
return (
time.getTime() < new Date(startDateTime).getTime() ||
time.getTime() > new Date(endDateTime).getTime()
);
},
selectableRange:
new Date(new Date().setHours(new Date().getHours() + 1)).format(
'hh'
) + ':00:00 - 23:59:00'
},selectableRange:可選時間段
startDateTime 時間戳
new Date(startDateTime) 標(biāo)準(zhǔn)時間
new Date(startDateTime).getTime() 時間戳
還需要watch監(jiān)聽和computed,如果不監(jiān)聽,那么每一天的當(dāng)前小時都會被禁用
computed: {
startTime() {
return this.editForm.startTime;
}
},
watch: {
startTime: function(newVal, oldVal) {
let selectDate = newVal.format('yyyyMMdd');
let oldDate = oldVal.format('yyyyMMdd');
let nowDate = new Date().format('yyyyMMdd');
// 如果兩次選擇的時間不相等
if (oldDate !== selectDate) {
// 如果這次選擇的時間等于今天的時間就不讓選當(dāng)前小時之前,否則就可以選全部時間段
if (selectDate === nowDate) {
this.pickerOptions.selectableRange =
new Date(new Date().setHours(new Date().getHours() + 1)).format(
'hh'
) + ':00:00 - 23:59:00';
} else {
this.pickerOptions.selectableRange = '00:00:00 - 23:59:00';
}
let realNewVal = new Date(
newVal.format('yyyy-MM-dd') + oldVal.format(' hh:mm:ss')
);
// 如果這個時間比當(dāng)前時間小,就等于當(dāng)前時間,否則等于這個參數(shù)的時間
if (realNewVal.getTime() < new Date().getTime()) {
this.editForm.startTime = new Date();
} else {
this.editForm.startTime = realNewVal;
}
}
}
},重寫了format方法,代碼如下:格式化時間也可以用庫,比如moment
/**
* 格式化日期
*/
export const dateFormat = function() {
/* eslint-disable no-extend-native */
Date.prototype.format = function(fmt) {
let o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'h+': this.getHours(), // 小時
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
S: this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + '').substr(4 - RegExp.$1.length)
);
}
for (let k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1
? o[k]
: ('00' + o[k]).substr(('' + o[k]).length)
);
}
}
return fmt;
};
};注意:Date 對象(Date 對象用于處理日期與時間)
new Date() 中國標(biāo)準(zhǔn)時間

到此這篇關(guān)于element日期時間選擇器限制時間選擇功能實現(xiàn)(精確到小時)的文章就介紹到這了,更多相關(guān)element日期時間選擇器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+elementUI實現(xiàn)多圖片上傳與回顯功能(含回顯后繼續(xù)上傳或刪除)
這篇文章主要介紹了Vue+elementUI實現(xiàn)多圖片上傳與回顯功能(含回顯后繼續(xù)上傳或刪除),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Vue中slot-scope的深入理解(適合初學(xué)者)
這篇文章主要給大家介紹了關(guān)于Vue中slot-scope的深入理解,這個教程非常適合初學(xué)者,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Vue循環(huán)組件加validate多表單驗證的實例
今天小編就為大家分享一篇Vue循環(huán)組件加validate多表單驗證的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue+Element-ui表單resetFields無法重置問題
本文主要介紹了Vue+Element-ui表單resetFields無法重置問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

