首頁  >  文章  >  web前端  >  如何在 JavaScript 中將日期轉換為另一個時區?

如何在 JavaScript 中將日期轉換為另一個時區?

王林
王林轉載
2023-09-08 23:17:021195瀏覽

如何在 JavaScript 中将日期转换为另一个时区?

JavaScript has a new Date() constructor which is used to create a date object to get the current date and time. This date object uses the UTC timezone or the ent brow'sclient brow's timez,ser i. if you are in India and use the new Date() constructor to get the date and time, you will get your local time. But sometimes, we may need to get the timezone of another country, which we can't do directly. These can be done using the toLocaleString() method or the format() method. By the end of the article, you will be able to get the date of any other timezone in JavaScript.

The two methods that we will use in this article to convert a date to another time zone are as follows −

  • Using toLocaleString() Method

  • #使用format()方法

使用 toLocaleString() 方法

toLocaleString() 方法可以使用日期物件呼叫。該方法具有根據傳入的參數將資料從一個時區轉換為另一個時區的能力。它接受兩個參數,第一個參數是“locale”,它是應該使用的格式約定的語言,對於英語來說是“en-US”,第二個參數是“options”,對我們來說是{timeZone:「countryName」},其中countryName是我們想要更改時區的國家的名稱。

以下是使用toLocaleString()方法在JavaScript中將日期轉換為另一個時區的逐步過程。

  • 使用Date建構子建立一個日期物件

  • Use the date object with toLocaleString() method and pass the first argument as 'en-US' for English language date and time formatting, and the second argument {timeZone: "America/New_York"} for getting the timezone of New York

  • Store the value return from this method into a variable, that variable is our required timezone.

#Example

在這個範例中,我們使用JavaScript的toLocaleString()方法將一個日期轉換為另一個時區。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Converting date to another timezone in JavaScript</title>
</head>
<body>
   <h3>Convert date to America/New_York Time Zone using toLocaleString() Method</h3>
   <p id="input">Local Time: </p>
   <p id="output">America/New_York Time Zone: </p>
   <script>
      // date objec
      let date = new Date();
      document.getElementById("input").innerText += date ;
      
      // convert date to another timezone
      let output = date.toLocaleString("en-US", {
         timeZone: "America/New_York"
      });
      
      // display the result
      document.getElementById("output").innerText += output;
   </script>
</body>
</html>

Using Format() Method

We can use the format() method with the "Intl.DateTimeFormat" object and use the date object passed as an argument to the format() method to convert the timezone to a timezone passed while creating() method to convert the timezone to a timezone passed while creating the "Intl.DateFormat" object. It sounds complicated but it is very simple if you look at the example below.

Here is the step-wise procedure to convert a date to another timezone in JavaScript using format() Method.

  • Create a date object using the Date constructor.

  • #在建立「Intl.DateTimeFormat」物件時,將第一個參數設為'en-US'以進行英文語言的日期和時間格式化,第二個參數{timeZone: "America/New_York "}用於取得紐約的時區。

  • Use the format() method with this object and pass the date object as an argument and store it in a variable, that variable is our required timezone.

################################################################################################################################################################################################ # ### ###Example### ###In this example, we are converting a date to another timezone in JavaScript using the format() Method.###
<!DOCTYPE html>
<html lang="en">
<head>
   <title>Convert date to America/New_York timezone in JavaScript</title>
</head>
<body>
   <h3>Convert date to America/New_York timezone using format() Method</h3>
   <p id="input">Local Time: </p>
   <p id="output">America/New_York Time Zone: </p>
   <script>
      // date objec
      let date = new Date();
      document.getElementById("input").innerText += date ;
      
      // create a new date object
      let newObj = Intl.DateTimeFormat('en-US', {
         timeZone: "America/New_York"
      })
      
      // convert date to another timezone
      let newDate = newObj.format(date);
      
      // display the result
      document.getElementById("output").innerHTML += newDate;
   </script>
</body>
</html>
###Summary### ###讓我們總結一下在本教程中學到的內容。我們看到我們有兩種方法可以將日期轉換為另一個時區,第一種是使用日期物件的toLocaleString()方法,第二種是使用"Intl.DateTimeFormat"物件的format()方法。這兩種方法有不同的用例,你可以根據自己的需求選擇。我們建議使用toLocaleString()方法,它易於使用,而且比使用"Intl.DateTimeFormat"物件的format()方法需要更少的程式碼行。 ###

以上是如何在 JavaScript 中將日期轉換為另一個時區?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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