验证日期+时间
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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