javascript数字转换为日期,字符串转换为日期,日期格式化
默认分类 2011-12-28 00:45:48
javascript 数字转换为日期, 字符串转换为日期, 日期格式化
demo
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Untitled 1</title>
- <script type="text/javascript">
- <!--
- function formatDate(date,fmt) {
- fmtfmt = fmt||'yyyy-MM-dd HH:mm';
- var o = {
- "M+" : date.getMonth()+1, //月份
- "d+" : date.getDate(), //日
- "h+" : date.getHours()%12 == 0 ? 12 : date.getHours()%12, //小时
- "H+" : date.getHours(), //小时
- "m+" : date.getMinutes(), //分
- "s+" : date.getSeconds(), //秒
- "q+" : Math.floor((date.getMonth()+3)/3), //季度
- "S" : date.getMilliseconds() //毫秒
- };
- var week = {
- "0" : "/u65e5",
- "1" : "/u4e00",
- "2" : "/u4e8c",
- "3" : "/u4e09",
- "4" : "/u56db",
- "5" : "/u4e94",
- "6" : "/u516d"
- };
- if(/(y+)/.test(fmt)){
- fmtfmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
- }
- if(/(E+)/.test(fmt)){
- fmtfmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[date.getDay()+""]);
- }
- for(var k in o){
- if(new RegExp("("+ k +")").test(fmt)){
- fmtfmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
- }
- }
- return fmt;
- }
- /*
- 将String类型解析为Date类型.
- parseDate('2006-1-1') return new Date(2006,0,1)
- parseDate(' 2006-1-1 ') return new Date(2006,0,1)
- parseDate('2006-1-1 15:14:16') return new Date(2006,0,1,15,14,16)
- parseDate(' 2006-1-1 15:14:16 ') return new Date(2006,0,1,15,14,16);
- parseDate('不正确的格式') retrun null
- */
- function parseDate(str){
- str = String(str).replace(/^[\s\xa0]+|[\s\xa0]+$/ig, '');
- var results = null;
-
- //秒数 #9744242680
- results = str.match(/^ *(\d{10}) *$/);
- if(results && results.length>0)
- return new Date(parseInt(str)*1000);
-
- //毫秒数 #9744242682765
- results = str.match(/^ *(\d{13}) *$/);
- if(results && results.length>0)
- return new Date(parseInt(str));
-
- //20110608
- results = str.match(/^ *(\d{4})(\d{2})(\d{2}) *$/);
- if(results && results.length>3)
- return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]));
-
- //20110608 1010
- results = str.match(/^ *(\d{4})(\d{2})(\d{2}) +(\d{2})(\d{2}) *$/);
- if(results && results.length>6)
- return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]));
-
- //2011-06-08
- results = str.match(/^ *(\d{4})[\._\-\/\\](\d{1,2})[\._\-\/\\](\d{1,2}) *$/);
- if(results && results.length>3)
- return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]));
-
- //2011-06-08 10:10
- results = str.match(/^ *(\d{4})[\._\-\/\\](\d{1,2})[\._\-\/\\](\d{1,2}) +(\d{1,2}):(\d{1,2}) *$/);
- if(results && results.length>6)
- return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]));
-
- //2011-06-08 10:10:10
- results = str.match(/^ *(\d{4})[\._\-\/\\](\d{1,2})[\._\-\/\\](\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);
- if(results && results.length>6)
- return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]),parseInt(results[6]));
-
- return null;
- }
-
- function doit(){
- //todo
- var s = Math.floor(new Date().getTime());
- document.getElementById('dd').innerHTML = s;
- alert(formatDate(parseDate(s)));
- }
-
- //-->
- </script>
- </head>
-
- <body><button type="button" onclick="doit()">doit</button>
- <div id="dd"></div>
- </body>
-
- </html>
>>
留言评论