利用日期函数配合正则表达式来检测日期时间格式,:
var checkTime = function (time) {
var timeFormat = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})\s(\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var result = false;
var dataFormat = time.match(timeFormat);
if (!dataFormat) return result;
var d = new Date(dataFormat[1], dataFormat[3] - 1, dataFormat[4]);
//检测日期
if (d.getFullYear() == dataFormat[1] && (d.getMonth() + 1) == dataFormat[3] && d.getDate() == dataFormat[4]) {
//检测时间
if (dataFormat[5] < 24 && dataFormat[6] < 60 && dataFormat[7] < 60) result = true;
}
return result;
}