ES6写法
时间戳转换成 “yyyy-MM-dd hh:mm:ss”格式
happenTimeFun(num){//时间戳数据处理
let date = new Date(num);
//时间戳为10位需*1000,时间戳为13位的话不需乘1000
let y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? ('0' + MM) : MM;//月补0
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;//天补0
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;//小时补0
let m = date.getMinutes();
m = m < 10 ? ('0' + m) : m;//分钟补0
let s = date.getSeconds();
s = s < 10 ? ('0' + s) : s;//秒补0
return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s;
},
另外一种补0写法
saveTiming(){//时间戳数据处理
let m=this;
let str=''
let date = new Date(m.value1);
str=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + m.checkTime(date.getHours()) + ':' + m.checkTime(date.getMinutes()) + ':' + m.checkTime(date.getSeconds());
return str
},
checkTime(time){//时间补零
let str=''
if(time<10){
str='0'+time
}else{
str=time
}
return str
},
“yyyy-MM-dd hh:mm:ss”转换成时间戳
timeProcessing(){
let timeDate = "2019-06-24 11:08:48";
let Time = new Date(timeDate);
console.log(Time)//Mon Jun 24 2019 11:08:48 GMT+0800 (中国标准时间)
let timestemp = Time.getTime();
console.log(timestemp)//1561345728000
}
网友评论