字符串格式为2021-04-18 12:23:26转为标准时间格式

字符串格式为2021-04-18 12:23:26转为标准时间格式
Sun Apr 18 2021 12:23:26 GMT+0800 (中国标准时间)

2021-4-18 12:23:26

关键代码:

this.datestring=‘2021-04-18 12:23:26’

 this.date=new Date(this.datestring.substr(0,4),  //年
 //new Date().getMonth(),
 this.datestring.substr(5,2)-1, //月
 this.datestring.substr(8,2), //日
 this.datestring.substr(11,2), //时
 this.datestring.substr(14,2), //分
 this.datestring.substr(17,2))//秒

 this.date1=this.GMTToStr(this.date)
},

mounted() {

},
methods:{
  GMTToStr(time){
let date = new Date(time)
let Str=date.getFullYear() + '-' +
(date.getMonth() + 1) + '-' +
date.getDate() + ' ' +
date.getHours() + ':' +
date.getMinutes() + ':' +
date.getSeconds()
return Str

}
}

你可能感兴趣的:(JS)