isValidDate = (year, month, day) => {
let dayAmount = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
let now = new Date()
//校验年份
// 年份应该是四位数字
if (isNaN(year) || year.length !== 4) {
return false;
}
// 年份应该在 1900 到 当前年 之间
const yearNum = parseInt(year);
let nowYear = now.getFullYear();
if (yearNum < 1900 || yearNum > nowYear) {
return false;
}
//判断是否是闰年
if (yearNum % 4 == 0 && yearNum % 100 != 0 || yearNum % 400 == 0) {
dayAmount[2] = 29
}
// 月份应该在 01 到 12 之间,并且在当月及之前
const monthNum = parseInt(month);
const nowMonth = now.getMonth() + 1;
if (monthNum < 1 || monthNum > 12 || nowYear === yearNum && monthNum > nowMonth) {
return false
}
//判断日是否合法
const dayNum = parseInt(day)
const nowDay = now.getDate()
if (monthNum === nowMonth && dayNum > nowDay || dayNum > dayAmount[monthNum]) {
return false
}
return true;
}
日期合法校验(1.0)
最新推荐文章于 2024-09-16 06:30:00 发布