js 格式化时间,可定义格式

 var format = function (time, format) { var t = new Date(time); var tf = function (i) { return (i < 10 ? '0' : '') + i }; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) { switch (a) { case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; } }) } 

format(new Date().getTime(), 'yyyy年MM月dd日 HH:mm:ss')
"2018年09月30日 09:27:47"

format(new Date().getTime(), 'yyyy/MM/dd HH:mm:ss')
"2018/09/30 09:27:03"

format(new Date().getTime(), 'yyyy:MM:dd HH:mm:ss')
"2018:09:30 09:27:21"

posted @ 2018-09-30 09:31  宋宇  阅读(543)  评论(0编辑  收藏  举报