现在的位置: 首页 > 综合 > 正文

JavaScrpt日期验证心得

2013年11月29日 ⁄ 综合 ⁄ 共 567字 ⁄ 字号 评论关闭
今天的项目中用到起止时间验证,限制时段为一个月,自已写了一小段代码,用得挺好,不敢独藏,大家看看吧!

// checkTwoDate(startDate,endDate)    startDate开始日期,endDate结束日期

function checkTwoDate(startDate,endDate){
var start_date = new Date(startDate.replace("-", ","));
var end_date = new Date(endDate.replace("-", ","));
var now_date=new Date();
if(start_date>now_date){
alert("开始日期为未来时,请重输入!");
return false;
}
if(end_date>now_date){
alert("结束日期为未来时,请重输入!");
return false;
}
if(start_date>end_date){
alert("起始日期不能大于终止日期!");
return false;
}
start_date.setMonth(start_date.getMonth()+1);
if(start_date<end_date){
alert("起始日期不能超过一个月!");
return false;
}
return true;
}

 

抱歉!评论已关闭.