1 /* 2 * 日期格式化 3 * =========*/ 4 function dateformat(date, format){ 5 var paddNum = function(num){ 6 num += ""; 7 return num.replace(/^(d)$/, "0$1"); 8 } 9 if(date == null){ 10 return date; 11 } 12 if(typeof date == 'string'){ 13 date = new Date(date.replace(/-/g, '/')); 14 }else if(typeof date == 'number'){ 15 date = new Date(date); 16 } 17 18 //时间格式字符 19 var cfg = { 20 yyyy: date.getFullYear(), 21 yy: date.getFullYear().toString().substring(2), 22 MM: paddNum(date.getMonth() + 1), 23 M: date.getMonth() + 1, 24 dd: paddNum(date.getDate()), 25 d: date.getDate(), 26 HH: paddNum(date.getHours()), 27 mm: paddNum(date.getMinutes()), 28 ss: paddNum(date.getSeconds()) 29 } 30 format || (format = 'yyyy-MM-dd HH:mm:ss'); 31 return format.replace(/([a-z])(1)*/ig, function(m){ 32 return cfg[m]; 33 }) 34 }