限制时分秒代码
startTime (index) {
const self = this
if (self.tableData[index].expirationTime) {
const startTime = self.tableData[index].effectiveTime
const endTime = self.tableData[index].expirationTime
const HH = new Date(endTime).getHours()
const MM = new Date(endTime).getMinutes()
const ss = new Date(endTime).getSeconds()
const isSameDay = new Date(startTime).toDateString() === new Date(endTime).toDateString()
return {
disabledDate (time) {
if (self.tableData[index].expirationTime) {
// 如果结束时间不为空,则小于结束时间
// 替换的目的(时间 2020-09-09 在IE中不支持; 2020/09/09 在IE中支持)
// self.formData.syncTimeE = self.formData.syncTimeE.replace(/-/g, '/')
const lastTime = new Date(self.tableData[index].expirationTime).getTime() // 选中的时间
if (lastTime < Date.now()) {
return lastTime < time.getTime()
} else {
// 开始时间不得超过当前天,如果开始时间可以大于当前时间,直接注释当前代码即可
return time.getTime() > Date.now()
}
} else {
return time.getTime() > Date.now() // 开始时间不选时,结束时间最大值小于等于当天
}
},
selectableRange: isSameDay ? '00:00:00 - ' + HH + ':' + MM + ':' + ss : '00:00:00 - 23:59:59'
}
}
},
endTime(index) {
const self = this
if (self.tableData[index].effectiveTime) {
const startTime = self.tableData[index].effectiveTime
const endTime = self.tableData[index].expirationTime
const HH = new Date(startTime).getHours()
const MM = new Date(startTime).getMinutes()
const ss = new Date(startTime).getSeconds()
console.log(HH, MM, ss)
const isSameDay = new Date(startTime).toDateString() === new Date(endTime).toDateString()
return {
disabledDate (time) {
if (self.tableData[index].effectiveTime) {
// return new Date(self.tableData[index].effectiveTime).getTime() - 86400 > time.getTime()
return HH === 0 && MM === 0 && ss === 0 ? new Date(startTime).getTime() > time.getTime() : new Date(startTime).getTime() - 86400000 > time.getTime()
}
},
selectableRange: isSameDay ? HH + ':' + MM + ':' + ss + ' - 23:59:59' : '00:00:00 - 23:59:59'
}
}
}
限制时分秒代码中有index参数,是因为我这段代码是在表格中做的处理,index算是个下标
return HH === 0 && MM === 0 && ss === 0 ? new Date(startTime).getTime() > time.getTime() : new Date(startTime).getTime() - 86400000 > time.getTime()
如果HH;MM;ss不为0,需要减去一天的毫秒数86400000,否则当天的日期不可选择