1 var Tools = {}; 2 Tools.formatDate = function (fmt,timestamp) { 3 if(timestamp){ 4 var date = new Date(parseInt(timestamp)); 5 }else{ 6 return ''; 7 } 8 var o = { 9 "M+": date.getMonth() + 1,//月份 10 11 "d+": date.getDate(),//日 12 13 "H+": date.getHours(), //小时 14 15 "h+": date.getHours(), 16 17 "m+": date.getMinutes(),//分 18 19 "s+": date.getSeconds(),//秒 20 21 "q+": Math.floor((date.getMonth() + 3) / 3),//季度 22 23 "S": date.getMilliseconds() //毫秒 24 }; 25 if (/(y+)/.test(fmt)){ 26 fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); 27 } 28 for (var k in o){ 29 if (new RegExp("(" + k + ")").test(fmt)){ 30 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); 31 } 32 } 33 return fmt; 34 }