公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

您永远不会在开发应用程序时使用 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>

登录后复制

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

Date() 对象的 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() 方法将小时设置为零后比较 date1 和 date2 。

示例

在下面的示例中,我们使用 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() 方法的第三种方法是非常简单的单行代码。

以上就是如何在 JavaScript 中检查同一天的两个时间戳?的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:两个 同一天 如何在 时间 检查
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定