js格林威治时间转换成正常时间
//时间转换 changeTime(time) {
//time 为 '2020-06-15T07:23:17.000+0000' let time2 = this.myTime(time); let time3 = this.formatDateTime(time2); return time3; //time3 为转换后的时间 2020-06-15 15:23:14 }, myTime(date) { var arr = date.split("T"); var d = arr[0]; var darr = d.split("-"); var t = arr[1]; var tarr = t.split(".000"); var marr = tarr[0].split(":"); var dd = parseInt(darr[0]) + "/" + parseInt(darr[1]) + "/" + parseInt(darr[2]) + " " + parseInt(marr[0]) + ":" + parseInt(marr[1]) + ":" + parseInt(marr[2]); return dd; }, addZero(num) { return num < 10 ? "0" + num : num; }, formatDateTime(date) { var time = new Date(Date.parse(date)); time.setTime(time.setHours(time.getHours() + 8)); // time.setTime(time.setHours(time.getHours())); var Y = time.getFullYear() + "-"; var M = this.addZero(time.getMonth() + 1) + "-"; var D = this.addZero(time.getDate()) + " "; var h = this.addZero(time.getHours()) + ":"; var m = this.addZero(time.getMinutes()) + ":"; var s = this.addZero(time.getSeconds()); return Y + M + D + h + m + s; }
方法2
function timeChange(time) { var date = time.substr(0, 10); //年月日 var hours = time.substring(11, 13); var minutes = time.substring(14, 16); var seconds = time.substring(17, 19); var timeFlag = date + ' ' + hours + ':' + minutes + ':' + seconds; timeFlag = timeFlag.replace(/-/g, "/"); timeFlag = new Date(timeFlag); timeFlag = new Date(timeFlag.getTime() + 8 * 3600 * 1000); timeFlag = timeFlag.getFullYear() + '-' + ((timeFlag.getMonth() + 1) < 10 ? "0" + (timeFlag.getMonth() + 1) : (timeFlag.getMonth() + 1)) + '-' + (timeFlag.getDate() < 10 ? "0" + timeFlag.getDate() : timeFlag.getDate()) + ' ' + timeFlag.getHours() + ':' + timeFlag.getMinutes() + ':' + (timeFlag.getSeconds() < 10 ? "0" + timeFlag.getSeconds() : timeFlag.getSeconds()); return timeFlag; };
调用
timeChange(2020-06-15T07:23:17.000+0000) //2020-06-15 15:23:14