毫秒数 --> 中国标准时间
new Date() // 当前时间:Fri Nov 08 2019 12:30:16 GMT+0800 (中国标准时间)
const time = new Date(1573187448465) // Fri Nov 08 2019 12:30:16 GMT+0800 (中国标准时间)
中国标准时间 --> 毫秒数
// getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数
let time = new Date(); // Fri Nov 08 2019 12:30:16 GMT+0800 (中国标准时间)
time = time.getTime(); // 1573187448465
毫秒数/中国标准时间 --> YY-MM-DD HH:MM:SS
let time = 1573187448465;
time = changeTime(time); 2019-11-08 12:30:48
function changeTime(time) {
const d = new Date(time)
const yy = d.getFullYear().toString()
let m = d.getMonth() + 1
let dd = d.getDate()
let hh = d.getHours()
let MM = d.getMinutes()
let ss = d.getSeconds()
m = m < 10 ? '0' + m : m.toString()
dd = dd < 10 ? '0' + dd : dd.toString()
hh = hh < 10 ? '0' + hh : hh.toString()
MM = MM < 10 ? '0' + MM : MM.toString()
ss = ss < 10 ? '0' + ss : ss.toString()
time = yy + '-' + m + '-' + dd + ' ' + hh + ':' + MM + ':' + ss
return time
}
备注:之所以在返回时间字符串前全部都.toString(),是因为移动端(实测ios)在没有.toString()的情况下,得到的time是NaN-NaN-NaN NaN:NaN:NaN。网上有人说是手机不能识别'-'这个字符导致的,待考究。