使用 JavaScript 将日期转换为另一个时区

使用 JavaScript 将日期转换为另一个时区

Convert a Date to another Time Zone using JavaScript

要将日期转换为另一个时区:

  1. 使用该toLocaleString()方法根据提供的时区获取表示日期的字符串。
  2. 将结果传递给Date()构造函数。
  3. 返回的Date对象将根据提供的时区设置其日期和时间。
索引.js
function changeTimeZone(date, timeZone) { if (typeof date === 'string') { return new Date( new Date(date).toLocaleString('en-US', { timeZone, }), ); } return new Date( date.toLocaleString('en-US', { timeZone, }), ); } const laDate = changeTimeZone(new Date(), 'America/Los_Angeles'); console.log(laDate); // 👉️ "Sun Jan 16 2022 01:22:07" const berlinDate = changeTimeZone(new Date(), 'Europe/Berlin'); console.log(berlinDate); // 👉️ "Sun Jan 16 2022 10:22:07"

toLocaleString
方法返回一个特定于区域设置的字符串,该字符串已根据提供的时区进行调整

changeTimeZone函数可以传递一个日期字符串或一个Date对象,并返回一个Date对象,该对象的日期和时间与提供的时区相对应。

我们传递给该toLocaleString方法的两个参数是:

  1. locales– 带有 BCP 47 语言标记的字符串或此类字符串的数组。您可以使用任何可用的语言环境,例如es-MX墨西哥或en-CA
    加拿大。
    如果您需要有关此参数的更多信息,请查看
    MDN 文档
  2. options我们在其中指定timeZone属性的对象。在MDN 文档中阅读有关该options对象的
    更多信息

您可以通过访问此维基百科页面找到国家代码和时区数据库名称的
表格

However, note that the Date object in JavaScript does not store a time zone.

It stores a timestamp that represents the number of milliseconds that have
passed since midnight on January 1st, 1970.

The date and time of the Date object correspond to the time zone, however the
Date object has no way of storing the specific time zone.

For this reason, it’s best to use the toLocaleString method to get a string that represents the time zone and use the options object parameter to format the string according to your needs.

You can use the different properties on the options object of the
toLocaleString method to format the date and time for the specific time zone
in different ways.

index.js
const date = new Date(); console.log( date.toLocaleString('en-US', { timeZone: 'America/New_York', dateStyle: 'full', timeStyle: 'full', }), ); // 👉️ "Sunday, January 16, 2022 at 4:42:40 AM Eastern Standard Time"

We set the dateStyle and timeStyle properties in the options object to
full to get a more verbose representation of the date and time.

Other possible values for the two properties are: long, medium and short.

index.js
const date = new Date(); console.log( date.toLocaleString('en-US', { timeZone: 'America/New_York', dateStyle: 'short', timeStyle: 'short', }), ); // 👉️ "1/16/22, 4:43 AM"

You can view all of the properties and values the options object implements by
visiting the
MDN docs.

Here is an example that shows the month, day, hours, minutes and seconds as
2-digits, even if their values are less than 10.

index.js
const date = new Date(); console.log( date.toLocaleString('en-US', { timeZone: 'America/New_York', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short', }), ); // 👉️ "01/16/2022, 04:43:39 AM EST"
By setting the values of the date and time components to 2 digits, we format them consistently, even if they have a value of less than 10.

If that’s the case, the values get padded with a leading zero.

我们还将该timeZoneName属性的值设置为short,以在结果末尾显示时区名称的缩写。

您可以通过访问MDN 文档options查看该对象支持的
所有其他属性

只需使用该方法,您就可以获得以多种不同方式格式化的时区特定日期和时间表示形式toLocaleString,这应该是您的首选方法,因为它利用了内置功能。