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


当前回答

你可以在new Date()上指定一个时区偏移量,例如:

new Date('Feb 28 2013 19:00:00 EST')

or

new Date('Feb 28 2013 19:00:00 GMT-0500')

由于Date存储UTC时间(即getTime返回UTC), javascript将它们转换为UTC时间,当你调用toString之类的东西时,javascript将把UTC时间转换为浏览器的本地时区,并返回本地时区的字符串,即如果我使用UTC+8:

> new Date('Feb 28 2013 19:00:00 GMT-0500').toString()
< "Fri Mar 01 2013 08:00:00 GMT+0800 (CST)"

你也可以使用普通的getHours/Minute/Second方法:

> new Date('Feb 28 2013 19:00:00 GMT-0500').getHours()
< 8

(这个8意味着在时间转换为我的本地时间- UTC+8后,小时数是8。)

其他回答

感谢@commonpike的回答,我写了一个函数,它接受ISO字符串日期,如2020-10-10T08:00:00.000作为输入,并发送一个包含2个主要属性的对象。

第一个是fromUtc,它是一个Date,对应于作为参数输入的timeZone。

第二个是toUtc,它允许您格式化源自fromUtc的Date。

const timeZoneTransformer = (stringDate, timeZone = "Europe/Paris") => { const now = new Date(); const serverDate = new Date(stringDate); const utcDate = new Date( Date.UTC( serverDate.getFullYear(), serverDate.getMonth(), serverDate.getDate(), serverDate.getHours(), serverDate.getMinutes(), serverDate.getSeconds() ) ); const invdate = new Date( serverDate.toLocaleString("en-US", { timeZone, }) ); const diff = now.getTime() - invdate.getTime(); const adjustedDate = new Date(now.getTime() - diff); return { toUtc: utcDate, fromUtc: adjustedDate, }; }; const fromUtc = timeZoneTransformer("2020-10-10T08:00:00.000").fromUtc; console.log(fromUtc); const toUtc = timeZoneTransformer(fromUtc).toUtc; console.log(toUtc);

我知道这3年太晚了,但也许它可以帮助其他人,因为除了moment-timezone库之外,我还没有找到任何类似的东西,这与他在这里要求的不完全相同。

我为德国时区做了类似的事情, 这有点复杂,因为日光节约时间和闰年,你有366天。

它可能需要与“isDaylightSavingTimeInGermany”函数做一些工作,而不同的时区在不同的日光节约时间发生变化。

不管怎样,看看这个页面: https://github.com/zerkotin/german-timezone-converter/wiki

主要方法有: convertLocalDateToGermanTimezone convertGermanDateToLocalTimezone

我已经努力记录了它,所以它不会那么令人困惑。

我在运行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);

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

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

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

我发现最受支持的方法是使用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函数将返回所需的结果。