Javascript 解析,格式化日期 (转)
将String类型解析为Date类型.
3 parseDate('2006-1-1') return new Date(2006,0,1)
4 parseDate(' 2006-1-1 ') return new Date(2006,0,1)
5 parseDate('2006-1-1 15:14:16') return new Date(2006,0,1,15,14,16)
6 parseDate(' 2006-1-1 15:14:16 ') return new Date(2006,0,1,15,14,16);
7 parseDate('2006-1-1 15:14:16.254') return new Date(2006,0,1,15,14,16,254)
8 parseDate(' 2006-1-1 15:14:16.254 ') return new Date(2006,0,1,15,14,16,254)
9 parseDate('不正确的格式') retrun null
10*/
11function parseDate(str){
12 if(typeof str == 'string'){
13 var results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) *$/);
14 if(results && results.length>3)
15 return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]));
16 results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);
17 if(results && results.length>6)
18 return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]),parseInt(results[6]));
19 results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9}) *$/);
20 if(results && results.length>7)
21 return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]),parseInt(results[6]),parseInt(results[7]));
22 }
23 return null;
24}
25
26/*
27 将Date/String类型,解析为String类型.
28 传入String类型,则先解析为Date类型
29 不正确的Date,返回 ''
30 如果时间部分为0,则忽略,只返回日期部分.
31*/
32function formatDate(v){
33 if(typeof v == 'string') v = parseDate(v);
34 if(v instanceof Date){
35 var y = v.getFullYear();
36 var m = v.getMonth() + 1;
37 var d = v.getDate();
38 var h = v.getHours();
39 var i = v.getMinutes();
40 var s = v.getSeconds();
41 var ms = v.getMilliseconds();
42 if(ms>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s + '.' + ms;
43 if(h>0 || i>0 || s>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
44 return y + '-' + m + '-' + d;
45 }
46 return '';
47}
48
3 parseDate('2006-1-1') return new Date(2006,0,1)
4 parseDate(' 2006-1-1 ') return new Date(2006,0,1)
5 parseDate('2006-1-1 15:14:16') return new Date(2006,0,1,15,14,16)
6 parseDate(' 2006-1-1 15:14:16 ') return new Date(2006,0,1,15,14,16);
7 parseDate('2006-1-1 15:14:16.254') return new Date(2006,0,1,15,14,16,254)
8 parseDate(' 2006-1-1 15:14:16.254 ') return new Date(2006,0,1,15,14,16,254)
9 parseDate('不正确的格式') retrun null
10*/
11function parseDate(str){
12 if(typeof str == 'string'){
13 var results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) *$/);
14 if(results && results.length>3)
15 return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]));
16 results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);
17 if(results && results.length>6)
18 return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]),parseInt(results[6]));
19 results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9}) *$/);
20 if(results && results.length>7)
21 return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]),parseInt(results[6]),parseInt(results[7]));
22 }
23 return null;
24}
25
26/*
27 将Date/String类型,解析为String类型.
28 传入String类型,则先解析为Date类型
29 不正确的Date,返回 ''
30 如果时间部分为0,则忽略,只返回日期部分.
31*/
32function formatDate(v){
33 if(typeof v == 'string') v = parseDate(v);
34 if(v instanceof Date){
35 var y = v.getFullYear();
36 var m = v.getMonth() + 1;
37 var d = v.getDate();
38 var h = v.getHours();
39 var i = v.getMinutes();
40 var s = v.getSeconds();
41 var ms = v.getMilliseconds();
42 if(ms>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s + '.' + ms;
43 if(h>0 || i>0 || s>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
44 return y + '-' + m + '-' + d;
45 }
46 return '';
47}
48