如何使用 JavaScript 检查输入日期是否等于今天的日期?

王林
王林 转载
2023-08-26 15:33:12 220浏览

如何使用 JavaScript 检查输入日期是否等于今天的日期?

在本教程中,我们学习检查输入日期是否等于今天的日期,或者不使用 JavaScript。有时,我们需要从输入字段中以各种格式获取用户的日期,并检查输入日期和今天的日期是否匹配。

在这里,我们将学习两种方法来检查输入日期与今天的日期是否相等。

分别比较年、月、日

用户可以使用 getFullYear()、getMonth() 和 getDate() 等各种方法从 Date 对象的时间戳中获取完整的年、月和日期。此外,我们可以通过用分隔符分割用户输入的字符串来获取年、月和日期。

之后,我们可以比较两个时间戳的年、月、日来检查输入日期是否等于今天的日期。

语法

用户可以按照下面的语法来匹配输入日期与今天的日期是否相等。

let inputYear = dateInput.value.split("//m.sbmmt.com/m/")[0];
let inputMonth = dateInput.value.split("//m.sbmmt.com/m/")[1];
let inputDate = dateInput.value.split("//m.sbmmt.com/m/")[2];
if (current_date.getFullYear() == inputYear && current_date.getMonth() == inputMonth - 1 && current_date.getDate(); == inputDate) {
   // date is equal to today’s date
} else{
   // date is not equal to today’s date
}

在上面的语法中,用“/”分割输入字符串后,我们得到一个包含三个值的数组。第一个值是年份,第二个值是月份,第三个值是日期。

算法

用户可以遵循以下算法。

  • 第 1 步 - 以特定格式从用户处获取日期。

  • 步骤 2 - 拆分日期字符串并提取年、月和日期。这里,我们使用了 JavaScript 的 split() 方法来分割日期字符串。

  • 第 3 步 - 使用 new Date() 构造函数创建一个等于今天日期的日期。

  • 第 4 步 - 使用 getFullYear()、getMonth() 和 getDate() 方法从今天的日期中提取年、月和日期。

  • 步骤 5 - 比较两个时间戳的年、月和日期。 getMonth() 方法返回 0 到 11 之间的月份。因此,我们需要将该月份与 inputMonth – 1 进行比较。

  • 第 6 步 - 如果两个日期的所有三个属性都匹配,则输入日期等于今天的日期。

示例

在下面的示例中,我们创建了compareDate() 函数。当用户按下提交按钮并在输入字段中输入日期字符串时,它会调用compareDate()函数。

compareDate() 函数实现上述算法来检查输入日期是否等于今天的日期。

<html>
<body>
   <h3>Comparing the <i>date, month, and year</i> separately to check if input the date is equal to today's date </h3>
   <h4>Enter the date in yyyy/mm/dd format.</h4>
   <input type="text" id="dateInput" />
   <button id = "submit" onclick = "compareDate()"> submit </button>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      let dateInput = document.getElementById("dateInput");
      function compareDate() {
         // split the input string
         let inputYear = dateInput.value.split("//m.sbmmt.com/m/")[0];
         let inputMonth = dateInput.value.split("//m.sbmmt.com/m/")[1];
         let inputDate = dateInput.value.split("//m.sbmmt.com/m/")[2];

         let current_date = new Date();
         // get the month, year, and date from the time stamp
         let year = current_date.getFullYear();
         let month = current_date.getMonth();
         let day = current_date.getDate();
         // compare year, month, and date
         if (year == inputYear && month == inputMonth - 1 && day == inputDate) {
            output.innerHTML = "Date is equal to today's date!";
         } else {
            output.innerHTML = "Date is not equal to today's date!";
         }
      }
   </script>
</body>
</html>

使用 toDateString() 方法

我们可以将 toDateString() 方法与 Date 对象的时间戳一起使用,该方法仅返回时间戳中的日期字符串。

我们可以使用从用户输入中获取的任何值来创建 Date 对象的新时间戳,并使用 toDateString() 方法获取日期字符串。接下来,我们还可以创建代表今天日期的当前日期并使用 toDateString() 方法。

最后,我们可以比较两个时间戳的日期字符串。

语法

用户可以按照以下语法使用 toDateString() 方法检查输入是否等于今天的日期。

let [year, month, date] = dateInput.value.split(",");
let inputDate = new Date(year, month - 1, date);
let current_date = new Date();
if (inputDate.toDateString() == current_date.toDateString()) {
   // it’s today’s date.
} else {
   // It’s not matching with today’s date
}

在上面的语法中,我们使用年、月 – 1 和日期值创建了输入日期的时间戳。由于 Date 对象的月份值介于 0 到 11 之间,因此我们需要提供月份 – 1。

示例

以下示例以特定格式在文本输入字段中从用户处获取日期字符串。之后,我们从字符串中提取年、月和日期并创建一个新日期。

接下来,我们比较两个时间戳的日期字符串。

<html>
<body>
   <h3>Using the <i> toDateString() </i> method to check if input the date is equal to today's date </h3>
   <h4>Enter the date in yyyy,mm,dd format.</h4>
   <input type = "text" id = "dateInput" />
   <button id = "submit" onclick = "compareDate()"> submit </button>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      let dateInput = document.getElementById("dateInput");
      function compareDate() {
         // extract the year, month, and date
         let [year, month, date] = dateInput.value.split(",");
         let inputDate = new Date(year, month - 1, date);
         let current_date = new Date();
         if (inputDate.toDateString() == current_date.toDateString()) {
            output.innerHTML = "Date is equal to today's date!";
         } else {
            output.innerHTML = "Date is not equal to today's date!";
         }
      }
   </script>
</body>
</html>

在上面的输出中,用户可以观察到以无效格式输入日期将在控制台中生成错误。

用户学习了两种方法来检查日期是否等于今天的日期。但是,用户也可以使用 setHours() 方法。我们可以在日期对象中将小时设置为 0,并将其与今天的日期进行比较以检查是否相等。

以上就是如何使用 JavaScript 检查输入日期是否等于今天的日期?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除