yyyy-MM-dd hh:mm:ss
=> 2017-06-29 09:20:43
function getNowFormatDate() { let date = new Date(dateStr); let seperator1 = '-'; let seperator2 = ':'; let month = date.getMonth() + 1; let strDate = date.getDate(); let hour = date.getHours(); let minute = date.getMinutes(); let second = date.getSeconds(); if (month >= 1 && month <= 9) { month = '0' + month; } if (strDate >= 0 && strDate <= 9) { strDate = '0' + strDate; } if (hour >= 0 && hour <= 9) { hour = '0' + hour; } if (minute >= 0 && minute <= 9) { minute = '0' + minute; } if (second >= 0 && second <= 9) { second = '0' + second; } let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + ' ' + hour + seperator2 + minute + seperator2 + second; return currentdate; } setInterval(() => { console.log(getNowFormatDate()) }, 1000)
or
let current_datetime = new Date() let formatted_date = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " " + current_datetime.getHours() + ":" + current_datetime.getMinutes() + ":" + current_datetime.getSeconds() console.log(formatted_date) // 2020-5-26 9:51:16