* format date. * * formatDate(new Date(),"yyyy-MM-dd hh:mm:ss") * formatDate(new Date().setHours(0,0,0,0),"yyyy-MM-dd hh:mm:ss") * * 更建议用类库: [moment.js](http://momentjs.com/) * * @param {Date/Number} obj date to format, support Date or timestamp * @param {String} [format] 格式 * @re
(obj,format)
| 18 | * @return {String} 格式化后的字符串 |
| 19 | */ |
| 20 | function formatDate(obj,format){ |
| 21 | var date = obj || new Date(); |
| 22 | if(obj && toString.call(obj) !== '[object Date]'){ |
| 23 | date = new Date(); |
| 24 | date.setTime(obj); |
| 25 | } |
| 26 | format = format || "yyyy-MM-dd hh:mm:ss"; |
| 27 | |
| 28 | var o = { |
| 29 | "M+" : date.getMonth()+1, //month |
| 30 | "d+" : date.getDate(), //day |
| 31 | "h+" : date.getHours(), //hour |
| 32 | "m+" : date.getMinutes(), //minute |
| 33 | "s+" : date.getSeconds(), //second |
| 34 | "q+" : Math.floor((date.getMonth()+3)/3), //quarter |
| 35 | "S" : date.getMilliseconds() //millisecond |
| 36 | } |
| 37 | if(/(y+)/.test(format)){ |
| 38 | format=format.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); |
| 39 | } |
| 40 | for(var k in o){ |
| 41 | if(new RegExp("("+ k +")").test(format)){ |
| 42 | format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); |
| 43 | } |
| 44 | } |
| 45 | return format; |
| 46 | } |
| 47 | |
| 48 | |
| 49 |
nothing calls this directly
no outgoing calls
no test coverage detected