日期组件的功能选中限制前后三个月-未来日期不支持选择
disabledDate: (time) => {
// 未来日期不支持选择
if (time.getTime() >= Date.now()) {
return true
}
const choiceDateTime = new Date(this.pickDate).getTime()
const minTime = new Date(choiceDateTime).setMonth(new Date(choiceDateTime).getMonth() - 3)
const maxTime = new Date(choiceDateTime).setMonth(new Date(choiceDateTime).getMonth() + 3)
const min = minTime
const max = maxTime
if (this.pickDate) {
return time.getTime() < min || time.getTime() > max || time.getTime() > (new Date()).getTime()
}
},
日期组件默认加载当天至一个月前时间
//在生命周期中使用
const nowDate = new Date()
const lastMonth = new Date(new Date().setDate(nowDate.getDate() - 30))
this.dateVal = [formatDateTime(lastMonth, 'yyyy-MM-dd') + '00:00:00', formatDateTime(nowDate, 'yyyy-MM-dd') + '23:59:59']
函数formatDateTime
export const formatDateTime = date => {
date = new Date(date)
var y = date.getFullYear()
var m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
var d = date.getDate()
d = d < 10 ? '0' + d : d
var h = date.getHours()
h = h < 10 ? '0' + h : h
var minute = date.getMinutes()
minute = minute < 10 ? '0' + minute : minute
var second = date.getSeconds()
second = second < 10 ? '0' + second : second
return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
}
//时间格式 转化为 20210723 传入的时间为标砖时间 Sat Jul 10 2021 00:00:00 GMT+0800 (中国标准时间)
export const dateTime = data => {
const date = new Date(data)
const y = date.getFullYear()
let m = date.getMonth() + 1
m = m < 10 ? ('0' + m) : m
let d = date.getDate()
d = d < 10 ? ('0' + d) : d
const time = y + '-' + m + '-' + d
const res = time
return res.split('-').join('')
}
|