首頁  >  文章  >  web前端  >  如何在 JavaScript 中檢查同一天的兩個時間戳記?

如何在 JavaScript 中檢查同一天的兩個時間戳記?

PHPz
PHPz轉載
2023-09-15 21:45:03861瀏覽

如何在 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>

將小時、分鐘、秒和毫秒設為零並比較兩個日期

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 中檢查同一天的兩個時間戳記?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除