原始代码
console.log(new Date(value.datetime))
- 转换失败!!!
问题解析:中文问题,导致转换失败
测试
console.log(typeof value.datetime)
console.log(new Date(value.datetime))
修复+封装
function Utils() { }
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
/**
* 格式化时间
* @param {date} 当前时间戳
* @return 返回的是已格式化的时间
*/
Utils.prototype.formatTime = date => {
let timeNum = date
if (typeof timeNum == "string") {
timeNum = parseInt(date)
}
let time = new Date(timeNum)
const year = time.getFullYear()
const month = time.getMonth() + 1
const day = time.getDate()
const hour = time.getHours()
const minute = time.getMinutes()
const second = time.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
module.exports = Utils