使用场景:Java后端返回前端的中国标准时间字符串,转换成y-m-d:h-m-s格式
time(date) { var y = date.getFullYear() var m = date.getMonth() + 1 m = m < 10 ? '0' + m : m var d = date.getDate() d = d < 10 ? '0' + d : d var h = date.getHours() h = h < 10 ? '0' + h : h var minute = date.getMinutes() minute = minute < 10 ? '0' + minute : minute var second = date.getSeconds() second = second < 10 ? '0' + second : second return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second },
使用方法如下:
//时间格式转换 var date = new Date("2020-05-12T09:18:51.000+0000"); var date1 = this.time(date); console.log(date1)