Skip to content

获取当日的日期

获取格式 格式 yyyy-MM-dd

js
 let date = new Date(),
    year = date.getFullYear(), //获取完整的年份(4位)
    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
    strDate = date.getDate() // 获取当前日(1-31)
if (month < 10) month = `0${month}` // 如果月份是个位数,在前面补0
if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
console.log(`${year}-${month}-${strDate}`,)
打印结果 : 2023-11-24
 let date = new Date(),
    year = date.getFullYear(), //获取完整的年份(4位)
    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
    strDate = date.getDate() // 获取当前日(1-31)
if (month < 10) month = `0${month}` // 如果月份是个位数,在前面补0
if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
console.log(`${year}-${month}-${strDate}`,)
打印结果 : 2023-11-24

反快餐主义者