验证日期+时间
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 function checkDateTime(date){
2 var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
3 var r = date.match(reg);
4 if(r == null){
5 alert("输入格式不正确,请按yyyy-MM-dd HH:mm:ss的格式输入!");
6 return false;
7 }else{
8 return true;
9 }
10 }
2 var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
3 var r = date.match(reg);
4 if(r == null){
5 alert("输入格式不正确,请按yyyy-MM-dd HH:mm:ss的格式输入!");
6 return false;
7 }else{
8 return true;
9 }
10 }
日期格式化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Date.prototype.format = function(format) //author: meizz
2 {
3 var o = {
4 "M+" : this.getMonth()+1, //month
5 "d+" : this.getDate(), //day
6 "h+" : this.getHours(), //hour
7 "m+" : this.getMinutes(), //minute
8 "s+" : this.getSeconds(), //second
9 "q+" : Math.floor((this.getMonth()+3)/3), //quarter
10 "S" : this.getMilliseconds() //millisecond
11 }
12 if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
13 (this.getFullYear()+"").substr(4 - RegExp.$1.length));
14 for(var k in o)if(new RegExp("("+ k +")").test(format))
15 format = format.replace(RegExp.$1,
16 RegExp.$1.length==1 ? o[k] :
17 ("00"+ o[k]).substr((""+ o[k]).length));
18 return format;
19 }
20 alert(new Date().format("yyyy-MM-dd"));
21 alert(new Date("january 12 2008 11:12:30").format("yyyy-MM-dd hh:mm:ss"));
22
2 {
3 var o = {
4 "M+" : this.getMonth()+1, //month
5 "d+" : this.getDate(), //day
6 "h+" : this.getHours(), //hour
7 "m+" : this.getMinutes(), //minute
8 "s+" : this.getSeconds(), //second
9 "q+" : Math.floor((this.getMonth()+3)/3), //quarter
10 "S" : this.getMilliseconds() //millisecond
11 }
12 if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
13 (this.getFullYear()+"").substr(4 - RegExp.$1.length));
14 for(var k in o)if(new RegExp("("+ k +")").test(format))
15 format = format.replace(RegExp.$1,
16 RegExp.$1.length==1 ? o[k] :
17 ("00"+ o[k]).substr((""+ o[k]).length));
18 return format;
19 }
20 alert(new Date().format("yyyy-MM-dd"));
21 alert(new Date("january 12 2008 11:12:30").format("yyyy-MM-dd hh:mm:ss"));
22
◑▂◐ ◑ω◐ ◑^◐ 初来置业,从一钉一铆开始 ◑△◐ ◑﹏◐ ◑0◐