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


当前回答

我也有同样的问题,但是我们可以使用我们想要的时区 我们使用。tolocaledatestring ()

例如: var day=new Date(); const选项={日:“数字”,月:“长”,年:“数字”,时区:“亚洲/加尔各答”}; const今天=。toLocaleDateString(“en-IN”,选项); console.log(今天);

其他回答

感谢@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);

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

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

这里有几个工作答案,但不知为何,他们中的很多人似乎让你到字符串,但不是回到一个日期对象,你开始,所以这里是我的简单的非函数采取如何在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.

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

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

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函数将返回所需的结果。