日期轉換格式
2020-06-27T14:20:27.000000Z 時間格式轉換成 2020-06-27 14:20:27
1 function rTime(date) { 2 var json_date = new Date(date).toJSON(); 3 return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') 4 } 5 let date = rTime('2020-06-27T14:20:27.000000Z'); 6 console.log(date) // 2020-06-27 14:20:27
toJSON() 方法可以將 Date 對象轉換為字符串,並格式化為 JSON 數據格式。
自己項目中的
1 // 時間過濾 2 this.List[i].date = this.List[i].requestUserTime; 3 4 function rTime(date) { 5 var json_date = new Date(date).toJSON(); 6 return new Date(+new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, 7 '') 8 9 } 10 11 this.List[i].date1 = rTime(this.List[i].date);
Sun Jul 04 2021 00:00:00 GMT+0800 (中國標准時間)轉2021-07-04 00:00
timeFormat(time) { // 時間格式化 2019-09-08 let year = time.getFullYear(); let month = time.getMonth() + 1 > 10 ? time.getMonth()+1 : '0'+(time.getMonth()+1); let day = time.getDate() >= 10 ? time.getDate() : "0" + time.getDate(); let h = time.getHours()>= 10 ? time.getHours() : "0" + time.getHours(); //獲取當前小時數(0-23) let m = time.getMinutes()>= 10 ? time.getMinutes() : "0" + time.getMinutes(); //獲取當前分鍾數(0-59) console.log(year + "-" + month + "-" + day+ " " + h+ ":" + m) return year + "-" + month + "-" + day+ " " + h+ ":" + m; },