日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

時區處理是每個 Web 應用程序的重要組成部分。后端記錄的時間通常采用UTC格式。然而,當它顯示給用戶時,必須將其轉換為用戶的本地時間。這可以通過 JavaScript 實現。我們將在本博客中了解如何使用 JavaScript 將 UTC 日期時間轉換為本地日期時間。

JavaScript 日期

JavaScript 包含一個“Date”類,允許我們處理日期和時間。 Date 類包含各種處理日期和時間的方法,包括 –

Date() – 以毫秒為單位返回當前日期和時間 getTime() 以毫秒為單位返回當前時間

getUTCFullYear() – 返回 UTC 時區的日期年份。

getUTCMonth() – 返回 UTC 時區中的日期月份。

getUTCDate() – 返回 UTC 時區中日期的月份和日期。

getUTCHours() – 返回 UTC 時區的日期小時。

getUTCMinutes() – 返回 UTC 時區的日期分鐘數。

getUTCSeconds() – 返回 UTC 時區中日期的秒數。

將 UTC 轉換為本地時間

我們必須使用 getTimezoneOffset() 方法將 UTC 日期時間轉換為本地日期時間。此方法返回 UTC 和本地時間之間的時差(以分鐘為單位)。然后可以使用此分鐘差異將 UTC 日期時間轉換為本地日期時間。

示例

例如,以下代碼將 UTC 日期時間轉換為本地日期時間 –

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var utc = new Date();
      var offset = utc.getTimezoneOffset();
      var local = new Date(utc.getTime() + offset * 60000);
      document.getElementById("result").innerHTML = local;
   </script> 
</body>
</html>

登錄后復制

我們可以在這里看到一個名為“utc”的 New Date 對象,它保存當前的 UTC 日期和時間。然后我們使用 getTimezoneOffset() 函數來計算 UTC 和本地時間之間的時差(以分鐘為單位)。最后,我們通過將此量與 UTC 時間(以毫秒為單位)相加來計算本地時間。

Similarly, we can convert a specified UTC date time into a local date time. To accomplish the same thing, simply supply the UTC date and time as parameters to the Date() function Object() { [native code] }. Now, let’s see the code to convert a UTC date time of “2018-11-12 12:00:00” to a local date time ?

示例

<!DOCTYPE html>
<html>
<head>
   <title>Date Example</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var utc = new Date("2018-11-12 12:00:00");
      var offset = utc.getTimezoneOffset();
      var local = new Date(utc.getTime() + offset * 60000);
      document.getElementById("result").innerHTML = "UTC : " + utc + "<br>" + "Local : " + local;
   </script>
</body>
</html>

登錄后復制

我們已將 UTC 日期和時間作為字符串傳遞給 Date() 構造函數。然后我們使用與之前相同的方法將 UTC 日期時間轉換為本地日期時間。

將當地時間轉換為 UTC

Now, how can we get from local time to UTC? To convert local date time to UTC date time, we can use the getTimezoneOffset() method once more. Since this function returns the time difference in minutes between UTC and local time. This difference number can be used to convert the local date time to the UTC date time.

示例

例如,以下代碼將本地日期時間轉換為 UTC 日期時間 –

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var local = new Date();
      var offset = local.getTimezoneOffset();
      var utc = new Date(local.getTime() - offset * 60000);
      document.getElementById("result").innerHTML = utc;
   </script>
</body>
</html>

登錄后復制

在上面的代碼中,我們首先創建了一個名為“local”的新 Date 對象,其中包含當前的本地日期和時間。然后,我們使用 getTimezoneOffset() 方法獲取 UTC 與本地時間之間的時差(以分鐘為單位)。從本地時間(以毫秒為單位)減去該值后,我們得到了 UTC 時間。

We can also convert a specific local date time into a UTC date time by passing the local date and time as arguments to the Date() constructor. For example, the following code will convert a local date time of “2018-11-12 12:00:00” into the UTC date time ?

示例

<!DOCTYPE html>
<html>
<body>
   <div id="result"></div>
   <script>
      var local = new Date("2018-11-12 12:00:00");
      var offset = local.getTimezoneOffset();
      var utc = new Date(local.getTime() - offset * 60000);
      document.getElementById("result").innerHTML = utc;
   </script> 
</body>
</html>

登錄后復制

我們將本地日期和時間作為字符串傳遞給 Date() 構造函數。然后我們使用與之前相同的方法將本地日期時間轉換為 UTC 日期時間。

結論

在本教程中,我們學習了如何使用 JavaScript 將 UTC 日期時間轉換為本地日期時間。我們還了解到,JavaScript Date 類提供了多種處理日期和時間的方法,例如 getTimezoneOffset(),可用于將 UTC 日期時間轉換為本地日期時間。我們還學習了如何使用本博客中的相同方法將本地日期時間轉換為 UTC 日期時間。需要注意的是,時區處理是任何 Web 應用程序的一個重要方面,正確轉換時間以便正確顯示給用戶也很重要。

以上就是如何使用 JavaScript 將 UTC 日期時間轉換為本地日期時間?的詳細內容,更多請關注www.92cms.cn其它相關文章!

分享到:
標簽:UTC 如何使用 日期 時間 轉換為
用戶無頭像

網友整理

注冊時間:

網站: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

通用課目體育訓練成績評定