JS代码格式化时间戳
一、[24小时制]yyyy-MM-dd HH:mm:ss
new Date().toJSON() // 2019-12-13T13:12:32.265Z
通过上面的方法,基本就可以将日期格式化,然后稍加处理就能得到预期结果
// new Date().getTime() 等价于 +new Date() function formartLongTime(time=+new Date()) { var date = new Date(time + 8 * 3600 * 1000); // 增加8小时得到东八区时间 return date.toJSON().substr(0, 19).replace(‘T‘, ‘ ‘); }
二、[12小时制]yyyy/MM/dd HH:mm:ss
new Date().toLocaleString() // 2019/12/13 下午9:24:43
同样,使用上述方法即可完成格式化
function formartLongTime(time=+new Date()) { return new Date(time).toLocaleString(); } // "2019/12/13 下午9:34:54"