When working on a project, the time data returned in the background is not in the form we want, such as (year – month – day hour: minute: second), so we have to write our own code to convert the format
The following code can quickly format the time, recommend favorites
The following code
let time = 'the 2020-04-24 T11:01:51. 000 z'
let tr = 1231231231231
// Define the formatting function
function formatTime(time) {
const timer = new Date(time)
const year = timer.getFullYear()
const mouth = timer.getMonth() + 1 > 10 ? timer.getMonth() + 1 : '0' + (timer.getMonth() + 1)
const date = timer.getDate() > 10 ? timer.getDate() : '0' + timer.getDate()
const hours = timer.getHours() > 10 ? timer.getHours() : '0' + timer.getHours()
const min = timer.getMinutes() > 10 ? timer.getMinutes() : '0' + timer.getMinutes()
const secon = timer.getSeconds() > 10 ? timer.getSeconds() : '0' + timer.getSeconds()
return year + The '-' + mouth + The '-' + date + ' ' + hours + ':' + min + ':' + secon
}
console.log(formatTime(time)) / / the 2020-04-24 19:01:51
console.log(formatTime(tr)) / / the 2009-01-06 16:40:31
Copy the code