第一种方法(固定格式转换):
Date.prototype.toLocaleString = function () { // 重写日期函数格式化日期
return `${this.getFullYear()}-${this.getMonth() + 1 >= 10 ? (this.getMonth() + 1) : '0' + (this.getMonth() + 1)}-${this.getDate() >= 10 ? this.getDate() : '0' + this.getDate()}
${this.getHours() >= 10 ? this.getHours() : '0' + this.getHours()} : ${this.getMinutes()>=10?this.getMinutes():'0'+this.getMinutes()} : ${this.getSeconds() >= 10 ? this.getSeconds() : '0' + this.getSeconds()}`;
};
使用方法:格式化后的日期 = new Date(这里是传入的毫秒值).toLocaleString();
vue中使用
var app = new Vue({
el: '#app',
data:{
time:""
},
created() {
this.time = this.dataFormat(new Date(1522611151000))
},