公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(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

通用课目体育训练成绩评定