就是把Date对象的日期部分转换为字符串,并返回结果。
代码示例:
console.log(new Date()) // 2023-01-10T05:42:41.926Z
console.log(new Date().toLocaleDateString()) // 2023/1/10
let time = new Date().toLocaleDateString()
let [year, month, date] = time.split('/')
console.log(year, month, date) // 2023 1 10
if(month < 10) {
month = '0' + month
}
if(date < 10) {
date = '0' + date
}
console.log(`${year}-${month}-${date}`) // 2023-01-10