校验表单时可能会遇到校验日期是否正确。能够利用JS的内置对象Date
帮助咱们完成日期校验。javascript
思路是首先用被校验日期(假设为A,可能为字符串或数字)建立一个Date
对象(假设为B)。
而后判断A和B的年、月、日是否所有相等。若是是,说明A是合法的;不然,A的范围有误。java
用代码表示为:函数
// 被校验日期A:2008年3月15日 var year = 2008, month = 3, date = 15; // 建立Date对象B var B = new Date(year, month-1, date); // 判断A和B的年月日是否所有相等 if (year === B.getFullYear() && month === B.getMonth()+1 && date === B.getDate()) { // 所有相等,被校验日期合法 } else { // 不所有相等,被校验日期的年、月、日至少有一个错误。 }
这里容易踩坑的地方是,Date
对象月份从零开始:0表示一月,1表示二月,...,11表示十二月。测试
为何要构建一个Date
对象?这就要涉及到Date
构造函数的语法了。code
官网上上给出了四种调用构造函数的形式:对象
new Date()
new Date(value)
new Date(dateString)
new Date(new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);
解释以下:ip
当拿到了被校验日期的年、月、日以后,计算毫秒数或构造日期格式字符串都比较麻烦。因此这里使用第四种。字符串
这时,调用Date
构造函数的另外一条规则就起做用了:若是参数值超过了合理范围,则它会被调整到临近值。我理解的就是,构造函数会为不合理的参数作进位运算。get
举个例子,new Date(2013,13,1)
至关于new Date(2014,1,1)
,也就是 2013年14月1日被进位成 2014年2月1日。it
再回到以前的校验代码:
// 被校验日期A:2013年14月1日 var year = 2013, month = 14, date = 1; // 建立Date对象B var B = new Date(2013, 13, 1); // B --> new Date(2014, 1, 1); // B.getFullYear() === 2014 // B.getMonth() === 1 // B.getDate() === 1 // 能够看到,year !== B.getFullYear() 而且 month !== B.getMonth() // 因此判断出被校验日期是非法的。
再加强一下咱们的程序,使之能够判断多种格式的被校验日期:
我写了一段简易的代码,仅供参考:
function isValidDate() { var year = 0, month = 0, date = 0; if (arguments.length === 3) { // 2008 3 15 // '2008' '3' '15' year = Number(arguments[0]); month = Number(arguments[1]); date = Number(arguments[2]); } else if (arguments.length === 2) { // '2008-03-15' '-' // '2008/3/15' '/' // 参数必须为字符串类型,分隔符不能为空字符串或数字 var str = arguments[0], seperator = arguments[1], dateArray = str.split(seperator); year = Number(dateArray[0]); month = Number(dateArray[1]); date = Number(dateArray[2]); } else if (arguments.length === 1) { // '20080315' // 20080315 // 参数必须为8位,且只能为数字 var str = String(arguments[0]); year = Number(str.slice(0, 4)); month = Number(str.slice(4, 6)); date = Number(str.slice(6)); } var dateObj = new Date(year, month-1, date), nYear = dateObj.getFullYear(), nMonth = dateObj.getMonth() + 1, nDate = dateObj.getDate(); if (year === nYear && month === nMonth && date === nDate) { return true; } else { return false; } } // 测试 isValidDate.assert = function(value) { if (value) { console.log('pass'); } else { console.log('failed'); } } isValidDate.test = function() { var assert = isValidDate.assert; assert(isValidDate(2008, 2, 29)===true); assert(isValidDate(2008, 3, 33)===false); assert(isValidDate(2017, 13, 01)===false); assert(isValidDate(2018, 5, 12)===true); assert(isValidDate('2008-2-29','-')===true); assert(isValidDate('2008/02/29','/')===true); assert(isValidDate('2008 03 17',' ')===true); assert(isValidDate('2008,13,01',',')===false); assert(isValidDate('2008,12,01',',')===true); assert(isValidDate('20080229')===true); assert(isValidDate('20081329')===false); assert(isValidDate('20081131')===false); assert(isValidDate(20080229)===true); assert(isValidDate(20081329)===false); assert(isValidDate(20081131)===false); } isValidDate.test();