简单描述:(待完善)
JS日期格式转换,通过正则表达式匹配中,将内容set入Date对象中
代码:
//兼容IE8
function parseDate(dateStr) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)(\s{1})(?:[01]\d|2[0-3])(?::[0-5]\d){2}\s*$/;//正则
var date = new Date(NaN);
var parts = isoExp.exec(dateStr);//正则验证
var result = dateStr;
if(parts) {
var month = Number(parts[2]);
//设置时间
date.setFullYear(parts[1], month - 1, parts[3]);
//判断是否正确
if(month != date.getMonth() + 1) {
date.setTime(NaN);
}
}
return date;
}