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

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

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

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 client browser’s timezone, i.e. 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 the “Intl.DateTimeFormat” 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 中將日期轉換為另一個時區?的詳細內容,更多請關注www.92cms.cn其它相關文章!

分享到:
標簽:中將 如何在 日期 時區 轉換為
用戶無頭像

網友整理

注冊時間:

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

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