介绍两种js将时间戳转化为日期的方法
1.使用toLocaleString
function getDate(timestamp) { return new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); } //timestamp是以秒为单位的时间戳 //例如 getDate(1611375369);//则输出2021/1/23 下午12:16
2.简单封装
function getDate(timestamp) { var time = new Date(timestamp * 1000); var year = time.getFullYear(); var month = time.getMonth() + 1; var date = time.getDate(); var hours = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); if (month < 10) { month = '0' + month }; if (date < 10) { date = '0' + date }; if (hours < 10) { hours = '0' + hours }; if (minute < 10) { minute = '0' + minute }; if (second < 10) { second = '0' + second }; return year + '/' + month + '/' + date + ' ' + hours + ':' + minute + ':' + second; } //timestamp是以秒为单位的时间戳 //例如 getDate(1611375369);//则输出2021/01/23 12:16:09