日常开发记录-js的Date对象中的toLocaleDateString()

就是把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

 

posted on 2023-01-10 13:50  法老的微笑  阅读(364)  评论(0)    收藏  举报