JavaScript 如何检查两个时间戳是否为同一天

JavaScript 如何检查两个时间戳是否为同一天

在开发应用程序时,使用JavaScript时几乎无法避免使用Date对象。Date对象在JavaScript中非常重要,它允许我们根据开发者的需求创建和操作日期。

在本教程中,我们将学习如何检查两个时间戳是否为同一天或不同天。在实时开发中非常有用。例如,我们希望用户执行一些每天的任务。因此,我们需要检查用户是否已经完成了今天的任务,我们可以通过比较任务最后完成的日期和当前日期来进行检查。

单独比较两个Date对象的年份、月份和日期

Date对象包含getFullYear()、getMonth()和getDate()方法,分别用于从日期值中获取年份、月份和日期。我们可以检查两个时间戳的年份、月份和日期是否相等,从而确定它们是否是同一天。

语法

用户可以按照以下语法使用getFullYear()、getMonth()、getDate()和相等运算符来检查两个时间戳是否为同一天。

if (
   date1.getFullYear() === date2.getFullYear() &&
   date1.getMonth() === date2.getMonth() &&
   date1.getDate() === date2.getDate()
) {

   // date is the same
} else {

   // date is not the same
} 

在上面的语法中,date1和date2是两个不同的时间戳。

示例

在下面的示例中,我们创建了三个名为date1、date2和date3的日期。我们创建了一个compareTwoDates()函数,该函数使用上述逻辑来比较两个时间戳是否在同一天。

<html>
<body>
   <h3>Compare the<i> year, month, and date </i> to check for two timestams of same day.</h3>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      var date1 = new Date();
      var date2 = new Date(date1.getTime() - 3000);
      function compareTwoDates(date1, date2) {

         // if the year, month, and date are the same, it means two dates are on the same day
         if (
            date1.getFullYear() === date2.getFullYear() &&
            date1.getMonth() === date2.getMonth() &&
            date1.getDate() === date2.getDate()
         ) {
            output.innerHTML += date1 + " and <br>" + date2 + " <br>are of same day. </br><br>";
         } else {
            output.innerHTML += date1 + " and <br>" + date2 + " <br>are not of same day. </br>";
         }
      }
      compareTwoDates(date1, date2);
      let date3 = new Date(2020, 11, 10);
      compareTwoDates(date1, date3);
   </script> 
</body>
</html>

将小时、分钟、秒和毫秒都设为零,并比较两个日期

setHours() 方法可以在时间戳中设置小时、分钟、秒和毫秒。它接受四个参数,分别代表小时、分钟、秒和毫秒。此外,最后三个参数是可选的,但我们将把它们都设为零。当我们将小时、分钟、秒和毫秒设为零时,我们可以得到一天开始的时间戳。如果两个时间戳的开始时间相同,则表示它们是同一天的时间戳。

语法

按照下面的语法来比较两个时间戳是否是同一天。

date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);

// compare timestamp
if (date1 == date2) {

   // date is the same
} else {

   // date is not the same
} 

在上面的语法中,我们使用setHours()方法将小时设置为零,然后比较 date1date2

示例

在下面的示例中,我们使用Date()对象创建了两个时间戳。compareTwoDates()函数通过将两个时间戳的小时、分钟、秒和毫秒设置为零来检查时间戳是否属于同一天。

<html>
<body>
   <h3>Seting<i> Hours, minutes, seconds, and milliseconds </i> to zero to check for two timestamps of the same day </h3>
   <p id="output"></p>
   <script>
      let output = document.getElementById("output");
      var date1 = new Date();
      var date2 = new Date(date1.getTime() - 3786000);
      function compareTwoDates(date1, date2) {

         // set hours, minutes, seconds, and milliseconds zero in the timestamp
         date1.setHours(0, 0, 0, 0);
         date2.setHours(0, 0, 0, 0);

         // compare timestamp
         if (date1 == date2) {
            output.innerHTML += date1 + " and <br>" + date2 + "<br> are of same day. </br>";
         } else {
            output.innerHTML += date1 + " and <br>" + date2 + "<br> are not of same day. </br>";
         }
      }
      compareTwoDates(date1, date2);
   </script>
</body>
</html>

使用toDateString()方法

toDateString()方法允许我们从时间戳中获取日期字符串,并从时间戳中删除时间,只返回日期字符串。如果两个时间戳的日期字符串相同,我们可以说它们是同一天。

语法

按照以下语法使用toDateString()方法来检查两个时间戳是否是同一天。

if (date1.toDateString() == date2.toDateString()) {

   // dates are of the same day
} else {

   // dates are not on the same day
}

示例

下面的范例中,当用户点击“比较两个日期”按钮时,它会调用 isForSameDays() 函数。在 isForSameDays() 函数内部,我们使用了toDateString()方法从时间戳中获取日期字符串,并使用等号运算符来比较两个日期字符串。

<html>
<body>
   <h3>Using the <i> toDateString() method </i> to check for two timestams of same day.</h3>
   <p id="output"></p>
   <script>
      let output = document.getElementById("output");
      var date1 = new Date();
      var date2 = new Date(2020, 01, 21, 12, 23, 22);

      // compare timestamp using the toDateString() method
      if (date1.toDateString() == date2.toDateString()) {
         output.innerHTML += date1 + " and " + date2 + " are of same day. </br>";
      } else {
         output.innerHTML += date1 + " and " + date2 + " are not of same day. </br>";
      }
   </script>
</body>
</html>

本教程向我们介绍了三种检查两个时间戳是否在同一天的方法。使用toDateString()方法的第三种方法非常简单,只需一行代码。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程