我有一个特定时区的日期时间作为字符串,我想将其转换为本地时间。但是,我不知道如何在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中将时间从不同的时区转换为本地时间?


当前回答

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

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

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

其他回答

我在运行GCP云函数时遇到了这个问题。当然,它在本地机器上工作,但是在云中运行使得new Date()的操作系统默认值(本地)无关紧要。在我的例子中,来自云的api调用需要东部标准时间,ISO格式(不带“Z”),偏移量为“-0500”或“-0400”,取决于DST,例如:

2021 - 12 - 01 - t00:00:00.000 - 0500

同样,这不是浏览器格式问题,所以我被迫采用这种格式,以便api调用能够正确工作。

使用@chickens代码作为开始,这是有效的:

var date = new Date(); 
var now_utc =  Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),
date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

var dt = new Date(now_utc);

let utcDate = new Date(dt.toLocaleString('en-US', { timeZone: "UTC" }));
let tzDate = new Date(dt.toLocaleString('en-US', { timeZone: "America/New_York" }));
let offset1 = utcDate.getTime() - tzDate.getTime();
let offset2 = offset1/60000;
let o1 = Math.abs(offset2);
console.log(offset2)
var offsetValue1 =  (offset2 < 0 ? "+" : "-") + ("00" + Math.floor(o1 / 60)).slice(-2) + ("00" + (o1 % 60)).slice(-2);
console.log(offsetValue1)
dt.setTime(dt.getTime() - offset1);

console.log(dt.toISOString());
console.log(dt.toISOString().slice(0,-1)+offsetValue1);

我发现最受支持的方法是使用getTimezoneOffset来计算适当的时间戳,或者更新时间,然后使用正常的方法来获得必要的日期和时间。

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

// ET timezone offset in hours.
var timezone = -5;
// Timezone offset in minutes + the desired offset in minutes, converted to ms.
// This offset should be the same for ALL date calculations, so you should only need to calculate it once.
var offset = (mydate.getTimezoneOffset() + (timezone * 60)) * 60 * 1000;

// Use the timestamp and offset as necessary to calculate min/sec etc, i.e. for countdowns.
var timestamp = mydate.getTime() + offset,
    seconds = Math.floor(timestamp / 1000) % 60,
    minutes = Math.floor(timestamp / 1000 / 60) % 60,
    hours   = Math.floor(timestamp / 1000 / 60 / 60);

// Or update the timestamp to reflect the timezone offset.
mydate.setTime(mydate.getTime() + offset);
// Then Output dates and times using the normal methods.
var date = mydate.getDate(),
    hour = mydate.getHours();

EDIT

我以前在执行日期转换时使用UTC方法,这是不正确的。通过将偏移量添加到时间上,使用本地get函数将返回所需的结果。

正如马特·约翰逊所说

如果你可以限制你的使用现代网络浏览器,你现在可以做到 以下没有任何特殊库: 新的日期()。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()}`);

尝试:date-from-timezone,它通过本地可用的Intl.DateTimeFormat来解析预期的日期。

我在我的一个项目中使用这种方法已经有几年了,但现在我决定把它作为一个小的OS项目发布:)

这应该解决您的问题,请随时提供修复。该方法还将考虑给定日期的夏时制。

dateWithTimeZone = (timeZone, year, month, day, hour, minute, second) => {
  let date = new Date(Date.UTC(year, month, day, hour, minute, second));

  let utcDate = new Date(date.toLocaleString('en-US', { timeZone: "UTC" }));
  let tzDate = new Date(date.toLocaleString('en-US', { timeZone: timeZone }));
  let offset = utcDate.getTime() - tzDate.getTime();

  date.setTime( date.getTime() + offset );

  return date;
};

如何与时区和本地时间一起使用:

dateWithTimeZone("America/Los_Angeles",2019,8,8,0,0,0)