js中时间戳转为日期格式
- 作者: 看我名字干啥
- 来源: 51数据库
- 2021-10-16
js中时间戳转为日期格式
时间戳转日期格式
function formatdatetime(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; } }) }
使用:
formatdatetime(new date(), "yyyy-mm-dd hh:mm:ss");
推荐阅读