最近的vue項目中遇到了一個時間格式2019-08-18T16:00:00.000Z,想把它轉換成2019-08-18 16:00:00的格式。前端
後端是python寫的,沒有找到好的轉換方式,最後決定在前端轉換,轉換方法代碼以下:vue
switchTimeFormat (time) { const dateTime = new Date(time) const year = dateTime.getFullYear() const month = dateTime.getMonth() + 1 const date = dateTime.getDate() const hour = dateTime.getHours() const minute = dateTime.getMinutes() const second = dateTime.getSeconds() return `${year}-${this.addZero(month)}-${this.addZero(date)} ${this.addZero(hour)}:${this.addZero(minute)}:${this.addZero(second)` }, addZero (v) { return v < 10 ? '0' + v : v }
最後轉換成經常使用的時間格式。python