日期格式化:yyyy-mm-dd的格式
  1. const now = new Date()
  2. const year = now.getFullYear()
  3. // 月份和日期 如果是一位前面给它填充一个0
  4. const month = (now.getMonth() + 1).toString().padStart(2, '0')
  5. const day = (now.getDate()).toString().padStart(2, '0')
  6. console.log(year, month, day)
  7. console.log( `${year}-${month}-${day}` ) //输入今天的日期 2021-12-31

数字替换(手机号,银行卡号等)
  1. const tel = '18781268679'
  2. const newTel = tel.slice(-4).padStart(tel.length, '*')
  3. console.log(newTel) // *******5678