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

 所属分类:web前端开发

 浏览:59次-  评论: 0次-  更新时间:2023-09-20
描述:更多教程资料进入php教程获得。 JavaScript has a new Date() constructor which is used to create a date object to...
更多教程资料进入php教程获得。

如何在 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 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()方法需要更少的代码行。

 标签:
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!