(function($){
$.extend({
IsDate : function(dateString,shortTime){
if(!dateString){
return false;
}
var testDate = new Date(dateString).valueOf();
if(!testDate){
return false;
}
if(testDate < 31507201000){
return false;
}
if(shortTime){
if(dateString.toString().indexOf(" ") ==-1){
return false;
}
}
if(dateString.toString().indexOf("/")!=-1){
dateString = dateString.replace(/[/]/g, '-');
}
var newDate = dateString.split(" ")[0].split("-");
if(dateString.toString().indexOf(" ")!=-1){
newDate = dateString.split(" ")[0].split("-");
}else{
newDate = dateString.toString().split("-");
}
var year = parseInt(newDate[0]);
var month = parseInt(newDate[1]);
var date = parseInt(newDate[2]);
if(year < date){
year = parseInt(newDate[2]);
month = parseInt(newDate[0]);
date = parseInt(newDate[1]);
}
var istrue = $.isValDate(year,month,date);
return istrue;
},
isValDate : function(year,month,date){
var months =[1,3,5,7,8,10,12];
if(months.indexOf(month)!=-1){
if(date>31){
return false;
}
}else{
if(date>30){
return false;
}
}
if(year%100 ==0 && year%400 ==0){
if(month == 2 && date>29){
return false;
}else{
return true;
}
}
if(year%100 !=0 && year%4 ==0){
if(month == 2 && date>29){
return false;
}else{
return true;
}
}
if(month == 2 && date>28){
return false;
}
return true;
}
});
})(jQuery);
例如
$.IsDate("2012-2-30") 返回 false;
$.IsDate("2012-2-25", true) 返回 false,第二个参数只要是验证日期中是否含有时间;
$.IsDate("2012-2-25 10:20 pm",true) 和$.IsDate("2012-2-25 10:20 pm") 返回的结果是一样的 都是 true;