如何将日期选择器数据转换为文本字符串?

问题描述 投票:0回答:1
html string forms text datepicker
1个回答
0
投票

要实现此目的,您可以使用 JavaScript 根据日期选择器中选择的值来更新 的内容。您需要向日期输入添加事件侦听器以检测更改,然后相应地更新内容。

<form action="/action_page.php">
  <label for="DayOW">Day of the week:</label>
  <input type="date" id="DayOW" name="DayOW" oninput="updateDate()">
  <div id="output">_______________</div>

  <script>
    function updateDate() {
      // Get the value of the date input
      var selectedDate = document.getElementById("DayOW").value;

      // Convert the selected date to the desired format (MM/DD/YYYY)
      var formattedDate = new Date(selectedDate);
      var mm = formattedDate.getMonth() + 1; // Months are zero-based
      var dd = formattedDate.getDate();
      var yyyy = formattedDate.getFullYear();

      // Update the content of the div with the formatted date
      var outputDiv = document.getElementById("output");
      outputDiv.textContent = mm + '/' + dd + '/' + yyyy;
    }
  </script>
</form>
© www.soinside.com 2019 - 2024. All rights reserved.