前端js时间戳转换日期格式
<script>
// 获取时间戳 Date.parse
const dateNumber = Date.parse(new Date())// 获取当前时间的时间戳
const dateStr = formatDate(dateNumber)//将时间戳转为日期格式
console.log('时间戳数字', dateNumber )
console.log('转换后的日期格式', dateStr)
function formatDate(date) {
var date = new Date(date);
var YY = date.getFullYear() + '-';
var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
return YY + MM + DD +" "+hh + mm + ss;
}
</script>

网友评论