JS中毫秒轉字串
阿新 • • 發佈:2018-11-28
來源於CSDN論壇,膜拜4L大神:https://bbs.csdn.net/topics/390374122
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; } }) } alert(format(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss'))