日期:2014-05-16  浏览次数:20372 次

js判断日期格式是否正确
function isDate(str){
  //如果是正确的日期格式返回true,否则返回false
  var regExp;
  regExp = /\b\d{4}-\d{1,2}-\d{1,2}\b/;
	//判断整体格式yyyy-mm-dd
	if (str!=str.match(/\d{4}-\d{2}-\d{2}/ig))	{
		alert('请输入日期格式为:yyyy-mm-dd!');
		return false;
	}

	var tmpArr;
	//tmpArr = str.split("-");
	tmpArr = str.split('-');
	
	var rYear,rMonth,rDay
	rYear = parseInt(tmpArr[0]);
	rMonth = parseInt(tmpArr[1]);
	rDay = parseInt(tmpArr[2]);
	//判断月
	//if ((rMonth > 12) || (rMonth < 1))	{
	//	alert('月份输入错误!');
	//	return false;
	//}

  var rYearflag;

	//判断润年
	if (((rYear%100) == 0) && ((rYear%4) == 0)){
	  rYearflag = true;
	}else if ((rYear%4) == 0){
	  rYearflag = true;
	}else{
	  rYearflag = false;
	}

	if (((",1,3,5,7,8,10,12,").indexOf(","+rMonth+",") != -1) && (rDay < 32)){
	  return(true);
	}	else if (((",4,6,9,11,").indexOf(","+rMonth+",") != -1) && (rDay < 31)){
	  return(true);
	}else if (rDay < 29){
    return(true);
	}else if (rYearflag && (rDay < 30)){
    return(true);
	}else {
    alert('天输入错误!');
		return false;
  }
}