首頁  >  文章  >  web前端  >  在 JavaScript 中取得日期之間的相對時間戳差異

在 JavaScript 中取得日期之間的相對時間戳差異

WBOY
WBOY轉載
2023-08-31 15:05:101368瀏覽

在 JavaScript 中获取日期之间的相对时间戳差异

您是否在任何網站上看到顯示時間戳記的通知?它顯示諸如“12 分鐘前”、“2 天前”、“10 小時前”等內容。它與兩個日期或時間之間的時間戳差異有關。

此外,某些應用程式顯示該裝置的上次登入時間是 22 小時前。因此,取得兩個日期之間的時間戳差異有很多用途。

在本教程中,我們將學習不同的方法來取得兩個日期之間的相對時間戳差異。

使用帶有日期的 getTime() 方法並建立自訂演算法

在 JavaScript 中,我們可以使用 new Date() 建構函數來建立日期物件。此外,我們可以將特定日期作為 Date() 建構函數的參數傳遞,以使用該日期值初始化日期物件。

getTime() 方法傳回 1970 年 1 月 1 日至今的總秒數。因此,我們可以找到兩個日期的總毫秒數,然後減去它們以獲得毫秒差。使用該毫秒,我們可以找到以秒、分鐘、年等為單位的時間戳差。

文法

使用者可以按照下面的語法來取得兩個日期之間的相對時間戳記差異。

let second_diff = (current_date.getTime() - previous_date.getTime())/1000; 

在上面的語法中,current_date 和 pervious_date 是兩個不同的日期。我們使用 getTime() 方法來取得兩個日期之間的毫秒差。

注意- 透過將 second_diff 變數的值與毫秒進行比較,您可以獲得相對時間戳記差異。

步驟

使用者可以按照以下步驟尋找不同單位(例如天、月、年等)的兩個日期之間的相對時間戳記。

第 1 步 - 建立兩個不同的日期。

步驟 2 - 使用 getTime() 方法取得兩個日期的總毫秒數並取得它們之間的差異。另外,將毫秒差除以 1000 將其轉換為秒,並將其儲存在 secondary_diff 變數中。

步驟 3 - 現在,使用 if-else 條件語句來找出相對時間戳差異。

步驟 4 - 如果 second_diff 的 值小於 60,則差異以秒為單位。 Second_diff 的值在 60​​ 到 3600 之間,差異以小時為單位。用戶還可以像這樣計算日、月、年。

範例

在下面的範例中,我們使用 Date 建構函數建立了兩個不同的日期對象,並使用上述步驟來尋找兩個日期之間的相對時間戳記。

在輸出中,使用者可以觀察到以下程式碼代表月份的時間戳記差異。

<html>
<body>
   <h3>Getting the relative timestamp difference between two dates using the <i> custom algorithm </i></h3>
   <p id="output"></p>
   <script>
      let output = document.getElementById("output");
      
      // creating the current date
      let current_date = new Date();
      
      // previous date
      let previous_date = new Date("jan 14, 2022 12:21:45");
      
      // finding the difference in total seconds between two dates
      let second_diff = (current_date.getTime() - previous_date.getTime()) / 1000;
      output.innerHTML += "The first date is " + current_date + "</br>";
      output.innerHTML += "The second date is " + previous_date + "</br>";
      
      // showing the relative timestamp.
      if (second_diff < 60) {
         output.innerHTML += "difference is of " + second_diff + " seconds.";
      } else if (second_diff < 3600) {
         output.innerHTML += "difference is of " + second_diff / 60 + " minutes.";
      } else if (second_diff < 86400) {
         output.innerHTML += "difference is of " + second_diff / 3600 + " hours.";
      } else if (second_diff < 2620800) {
         output.innerHTML += "difference is of " + second_diff / 86400 + " days.";
      } else if (second_diff < 31449600) {
         output.innerHTML += "difference is of " + second_diff / 2620800 + " months.";
      } else {
         output.innerHTML += "difference is of " + second_diff / 31449600 + " years.";
      }
   </script>
</body>
</html>

使用Intl的RelativeTimeFormat() API

Intl是指國際化API。它包含各種日期和時間格式化方法。我們可以使用 Intl 物件的 RelativeTimeFormat() 方法來取得兩個日期之間的相對時間戳記。

文法

使用者可以依照下列語法使用RelativeTimeFormat() API 來取得兩個日期之間的相對時間戳記。

var relativeTimeStamp =
new Intl.RelativeTimeFormat("en", { numeric: "auto",});

// compare the value of RelativeTimeStamp with milliseconds of different time units

在上述語法中,RelativeTimeFormat() 方法傳回時間戳記差異。 time_Stamp_unit 是一個包含不同時間單位及其總毫秒數的物件。

步驟

第 1 步 - 建立一個單位對象,其中包含時間單位作為鍵,總毫秒數作為該時間單位的值。

步驟 2 - 取得兩個日期之間的時間差(以毫秒為單位)。

第3 步驟 - 現在使用for-in 迴圈迭代time_stamp_unit 物件並檢查second_diff 的值是否大於特定時間的總毫秒數;使用RelativeTimeFormat() API 的format 方法來格式化該特定單位的時間戳記。

第 4 步 - 之後,中斷 for 迴圈。

範例

在下面的範例中,我們使用 RelativeTimeFomrat() 方法來取得兩個日期之間的相對時間戳記差異,如上述語法和步驟所述。

<html>
<body>
   <h3>Getting the relative timestamp difference between two dates using the <i> RelativeTimeFormat() </i> method </h3>
   <p id="output"></p>
   <script>
      let output = document.getElementById("output");
      let current_date = new Date();
      let previous_date = new Date("jan 14, 2022 12:21:45");
      
      // finding the difference in total seconds between two dates 
      let second_diff = current_date.getTime() - previous_date.getTime();
      output.innerHTML += "The first date is " + current_date + "</br>";
      output.innerHTML += "The second date is " + previous_date + "</br>";
      var time_Stamp_unit = {
         year: 31536000000,
         month: 31536000000 / 12,
         day: 86400000,
         hour: 3600000,
         minute: 60000,
         second: 1000,
      };
      var relativeTimeStamp = new Intl.RelativeTimeFormat("en", {
         numeric: "auto",
      });
      
      // iterate through all time stamps
      for (var ele in time_Stamp_unit) {
         
         // if second_diff's value is greater than particular timesapm unit's total millisecond value, format accordingly
         if (Math.abs(second_diff) > time_Stamp_unit[ele] || ele == "second") {
            output.innerHTML += "The difference between two dates is " + relativeTimeStamp.format(
               Math.round(second_diff / time_Stamp_unit[ele]), ele
            );
            break;
         }
      }
   </script>
</body>
</html>

使用者學會了使用 if-else 語句和 RelativeTimeFormat() API 的 format() 方法來找出兩個日期之間的相對時間戳記。使用者可以根據自己的需求使用這兩種方法。

以上是在 JavaScript 中取得日期之間的相對時間戳差異的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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