時區處理是每個 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其它相關文章!