月日の頭に0を付けてtype="date"のvalueに送るサンプル

<p><input type="date" id="date" value=""></p>
<p><button id="btn">本日の日付を反映する</button></p>
const btn = document.getElementById('btn')
const date = document.getElementById('date')

btn.addEventListener('click', () => {
  const now = new Date()
  const formatDate = `${now.getFullYear()}-${('0' + (now.getMonth() + 1)).slice(-2)}-${('0' + now.getDate()).slice(-2)}`
  console.log({formatDate})
  date.value = formatDate
})

元記事を表示する