我有一个特定时区的日期时间作为字符串,我想将其转换为本地时间。但是,我不知道如何在Date对象中设置时区。

例如,我有2013年2月28日东部时间晚上7点,然后我可以

var mydate = new Date();
mydate.setFullYear(2013);
mydate.setMonth(02);
mydate.setDate(28);
mydate.setHours(7);
mydate.setMinutes(00);  

据我所知,我可以设置UTC时间或本地时间。但是,如何在另一个时区设置时间呢?

我尝试使用添加/减去UTC的偏移量,但我不知道如何对抗夏令时。我不确定我走的方向是否正确。

如何在javascript中将时间从不同的时区转换为本地时间?


当前回答

这里有几个工作答案,但不知为何,他们中的很多人似乎让你到字符串,但不是回到一个日期对象,你开始,所以这里是我的简单的非函数采取如何在JS日期更改时区:

var TZ='Australia/Brisbane'; //Target timezone from server
var date = new Date();       //Init this to a time if you don't want current time
date=new Date(Date.parse(date.toLocaleString("en-US", {timeZone: TZ})));
//Just a clarification on what happens
// 1) First new Date() gives you a Date object at current time in the clients browser local timezone
// 2) .toLocaleString takes that time, and returns a string if time in the target timezone
// 3) Date.parse converts that new string to a Unix epoch number
// 4) new Date() converts the Unix epoch into a Date object in the new TimeZone. 
// Now I can use my usual getHours and other Date functions as required.

希望这对其他人有所帮助(如果你找到了下面的答案!)

其他回答

尝试使用npm中的ctoc。 https://www.npmjs.com/package/ctoc_timezone

它有简单的功能来改变时区(大多数时区大约400)和你想要显示的所有自定义格式。

遇到了同样的问题,用了这个吗

Console.log(Date.parse(“Jun 13, 2018 10:50:39 GMT+1”));

它将返回毫秒,你可以检查有+100 timzone初始化英国时间 希望能有所帮助!!

我在单元测试中遇到了类似的问题(特别是开玩笑地说,单元测试在本地运行以创建快照,然后CI服务器(可能)在不同的时区运行,导致快照比较失败)。我嘲笑我们的约会和一些辅助方法,如下所示:

describe('...', () => {
  let originalDate;

  beforeEach(() => {
    originalDate = Date;
    Date = jest.fn(
      (d) => {
        let newD;
        if (d) {
          newD = (new originalDate(d));
        } else {
          newD = (new originalDate('2017-05-29T10:00:00z'));
        }
        newD.toLocaleString = () => {
          return (new originalDate(newD.valueOf())).toLocaleString("en-US", {timeZone: "America/New_York"});
        };
        newD.toLocaleDateString = () => {
          return (new originalDate(newD.valueOf())).toLocaleDateString("en-US", {timeZone: "America/New_York"});
        };
        newD.toLocaleTimeString = () => {
          return (new originalDate(newD.valueOf())).toLocaleTimeString("en-US", {timeZone: "America/New_York"});
        };
        return newD;
      }
    );
    Date.now = () => { return (Date()); };
  });

  afterEach(() => {
    Date = originalDate;
  });

});

我不知道为什么所有这些答案都如此复杂。在本地/所需时区中创建仅限日期的日期时,只需使用YYYY-MM-DD ZZZ。

创建一个本地日期:

var myDate = new Date('2022-11-29 CST')

日期将以UTC格式存储在存储器中,很好。

从存储中获取日期并显示为本地:

myDate.toLocaleDateString()

11/29/2022

正如马特·约翰逊所说

如果你可以限制你的使用现代网络浏览器,你现在可以做到 以下没有任何特殊库: 新的日期()。toLocaleString("en-US", {timeZone: "America/New_York"})

这不是一个全面的解决方案,但它适用于许多场景 只需要输出转换(从UTC或本地时间到) 具体时区,但没有其他方向)。

所以尽管浏览器在创建日期时不能读取IANA时区,或者有任何方法来更改现有date对象上的时区,但似乎有一个hack围绕它:

function changeTimezone(date, ianatz) { // suppose the date is 12:00 UTC var invdate = new Date(date.toLocaleString('en-US', { timeZone: ianatz })); // then invdate will be 07:00 in Toronto // and the diff is 5 hours var diff = date.getTime() - invdate.getTime(); // so 12:00 in Toronto is 17:00 UTC return new Date(date.getTime() - diff); // needs to substract } // E.g. var here = new Date(); var there = changeTimezone(here, "America/Toronto"); console.log(`Here: ${here.toString()}\nToronto: ${there.toString()}`);