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

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

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員: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() 方法可以使用日期對(duì)象調(diào)用。該方法具有根據(jù)傳入的參數(shù)將數(shù)據(jù)從一個(gè)時(shí)區(qū)轉(zhuǎn)換為另一個(gè)時(shí)區(qū)的能力。它接受兩個(gè)參數(shù),第一個(gè)參數(shù)是“l(fā)ocale”,它是應(yīng)該使用的格式約定的語(yǔ)言,對(duì)于英語(yǔ)來(lái)說(shuō)是“en-US”,第二個(gè)參數(shù)是“options”,對(duì)于我們來(lái)說(shuō)是{timeZone:“countryName”},其中countryName是我們想要更改時(shí)區(qū)的國(guó)家的名稱。

    以下是使用toLocaleString()方法在JavaScript中將日期轉(zhuǎn)換為另一個(gè)時(shí)區(qū)的逐步過(guò)程。

      使用Date構(gòu)造函數(shù)創(chuàng)建一個(gè)日期對(duì)象

      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

      在這個(gè)例子中,我們使用JavaScript的toLocaleString()方法將一個(gè)日期轉(zhuǎn)換為另一個(gè)時(shí)區(qū)。

      <!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>
      

      登錄后復(fù)制

      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.

        在創(chuàng)建“Intl.DateTimeFormat”對(duì)象時(shí),將第一個(gè)參數(shù)設(shè)置為’en-US’以進(jìn)行英語(yǔ)語(yǔ)言的日期和時(shí)間格式化,第二個(gè)參數(shù){timeZone: “America/New_York”}用于獲取紐約的時(shí)區(qū)。

        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>
        

        登錄后復(fù)制

        Summary

        讓我們總結(jié)一下在本教程中學(xué)到的內(nèi)容。我們看到我們有兩種方法可以將日期轉(zhuǎn)換為另一個(gè)時(shí)區(qū),第一種是使用日期對(duì)象的toLocaleString()方法,第二種是使用”Intl.DateTimeFormat”對(duì)象的format()方法。這兩種方法有不同的用例,你可以根據(jù)自己的需要選擇。我們推薦使用toLocaleString()方法,它易于使用,而且比使用”Intl.DateTimeFormat”對(duì)象的format()方法需要更少的代碼行。

        以上就是如何在 JavaScript 中將日期轉(zhuǎn)換為另一個(gè)時(shí)區(qū)?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!

分享到:
標(biāo)簽:中將 如何在 日期 時(shí)區(qū) 轉(zhuǎn)換為
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定