考虑了大小月份,考虑了闰年匹配YYYY-MM-DD[code]
(([0-9]{3}[1-9])-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
[/code]匹配DD/MM/YYYY
[code]
(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{3}[1-9]))|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))
[/code]详细解释: http://bpwang.blogspot.com/2007/03/blog-post.html
(([0-9]{3}[1-9])-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
[/code]匹配DD/MM/YYYY
[code]
(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{3}[1-9]))|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))
[/code]详细解释: http://bpwang.blogspot.com/2007/03/blog-post.html
YYYY-MM-DD(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
if(!/\d{4}-\d{1,2}-\d{1,2}/.test(t))
return false;
var MonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var tempArray = t.split("-");
var year = tempArray[0];
var month = tempArray[1];
var day = tempArray[2];
if(year <=0 || year > 9999)
return false;
if(month <=0 || month > 12)
return false;
var rightDay = 0;
if(month == 2)
rightDay = ((year%4 == 0) && (year%100 != 0)||(year%400 == 0)) ? 29 : 28;
else
rightDay = MonthDays[month - 1];
if(day <= 0 || day > rightDay)
return false;
return true;
};
<script language=javascript>
String.prototype.isTime = function()
{
var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2})$/);
if(r==null)return false; var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],0);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]);
}
alert("2002-1-31 12:34".isTime());
alert("2001-2-29 12:54".isTime());
alert("2002-1-41 12:00".isTime());
</script>
Date.prototype.format="yyyy-MM-dd hh:mm:ss";
//used for cache the RegularExpressions
Date.prototype.Regex={
"regex1" : /([\$\(\)\*\+\.\[\]\?\\\^\{\}\|])/g ,
"regex2" : /(y+|M+|d+|m+|h+|s+)/g ,
"year1" : /^y{1,2}$/ ,
"year2" : /^y{4}$/ ,
"month" : /^M{1,2}$/ ,
"day" : /^d{1,2}$/ ,
"hour" : /^h{1,2}$/i ,
"minute" : /^m{1,2}$/ ,
"second" : /^s{1,2}$/
}
//[extended method] Date.setDate by format string
//Warning : date string must perfect than format string
Date.prototype.setFormatDate = function() //author : gzdiablo
{
var A = arguments;
var inputStr = "";
var sformat = this.format;
if(A.length==0 && A.length>2){return this;}
if(A.length==1){inputStr = A[0];}
if(A.length==2){inputStr = A[0];sformat = A[1];}
if(sformat == null && inputStr == null)return this;
var tempint = Date.parse(inputStr);
if(!isNaN(tempint))this.setTime(tempint);
var tempregex = new RegExp(sformat.replace(this.Regex["regex1"],"\\$1").replace(this.Regex["regex2"],"(\\d+)"));
var temparr = sformat.match(this.Regex["regex2"]);
var temparr2 = inputStr.match(tempregex);
if(temparr2!=null && temparr!=null)
{
if(temparr2.length>1)temparr2.shift();
for(var i=0;i<temparr.length && temparr2[i]!=null;i++)
{
switch(true)
{
case this.Regex["year1"].test(temparr[i]):this.setFullYear(temparr2[i]*1 + 2000);break;
case this.Regex["year2"].test(temparr[i]):this.setFullYear(temparr2[i]*1);break;
case this.Regex["month"].test(temparr[i]):this.setMonth(temparr2[i]*1-1);break;
case this.Regex["day"].test(temparr[i]):this.setDate(temparr2[i]*1);break;
case this.Regex["hour"].test(temparr[i]):this.setHours(temparr2[i]*1);break;
case this.Regex["minute"].test(temparr[i]):this.setMinutes(temparr2[i]*1);break;
case this.Regex["second"].test(temparr[i]):this.setSeconds(temparr2[i]*1);break;
}
}
}
return this;
}这样试试 使用方法
使用方法1:
var aa = new Date();
aa.format = "yyyy-MM-dd hh:mm:ss";
aa.setFormatDate("2007-3-2 19:30:20");
alert(aa.toLocaleString());
使用方法2:
var aa = new Date();
aa.setFormatDate("2007年3月2日 19时30分20秒","yyyy年MM月dd日 hh时mm分ss秒");
alert(aa.toLocaleString());