我正在寻找一个函数转换日期在一个时区到另一个。

它需要两个参数,

日期(格式为“2012/04/10 10:10:30 +0000”) 时区字符串("Asia/Jakarta")

时区字符串在http://en.wikipedia.org/wiki/Zone.tab中描述

有什么简单的方法吗?


当前回答

有一个npm模块叫做timezones。Json你可以使用它。它基本上由一个json文件和包含夏令时和偏移量信息的对象组成。

对于asia/jakarta,它可以返回这个对象:

{
  "value": "SE Asia Standard Time",
  "abbr": "SAST",
  "offset": 7,
  "isdst": false,
  "text": "(UTC+07:00) Bangkok, Hanoi, Jakarta",
  "utc": [
    "Antarctica/Davis",
    "Asia/Bangkok",
    "Asia/Hovd",
    "Asia/Jakarta",
    "Asia/Phnom_Penh",
    "Asia/Pontianak",
    "Asia/Saigon",
    "Asia/Vientiane",
    "Etc/GMT-7",
    "Indian/Christmas"
  ]
}

你可以在这里找到它:

https://github.com/dmfilipenko/timezones.json

https://www.npmjs.com/package/timezones.json

希望对大家有用

其他回答

我不知道一个简单的方法来转换日期对象到任何时区,但如果你想把它转换到本地时区,你可以用date .prototype. gettime()转换到相应的毫秒数,然后再回来。

let date0 = new Date('2016-05-24T13:07:20'); let date1 = new Date(date0.getTime()); console.log(“$ {date0} \ n $ {date1} ');

例如,date.getHours()现在将返回15而不是13,如果你像我一样在奥地利(而且是夏天)。

我读到过,各种datetime函数在某些浏览器中可能会表现出非标准的行为,所以先测试一下。我可以确认它在Chrome中工作。

所有这些答案都有点多余,但这对我来说是有效的,可以获得具有特定小时偏移量的当前Date对象。

function hourToMs(hour) { return hour * 60 * 1000 * 60; } function minToMs(min) { return min * 60 * 1000; } function getCurrentDateByOffset(offset) { // Get the current timezone in milliseconds to reset back to GMT aka +0 let timezoneOffset = minToMs((new Date()).getTimezoneOffset()); // get the desired offset in milliseconds, invert the value because javascript is dum let desiredOffset = hourToMs(offset * -1); return new Date(Date.now() + timezoneOffset - desiredOffset); } // -6 hours is central timezone console.log("The time is: " + getCurrentDateByOffset(-6));

无耻地窃取自:http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329

/** 
 * function to calculate local time
 * in a different city
 * given the city's UTC offset
 */
function calcTime(city, offset) {

    // create Date object for current location
    var d = new Date();
   
    // get UTC time in msec
    var utc = d.getTime();
   
    // create new Date object for different city
    // using supplied offset
    var nd = new Date(utc + (3600000*offset));
   
    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();
}

这个函数通过提供城市/国家的名称和偏移值来计算时区值

好了,找到了!

我使用的是timezone-js。这是代码:

var dt = new timezoneJS.Date("2012/04/10 10:10:30 +0000", 'Europe/London');
dt.setTimezone("Asia/Jakarta");

console.debug(dt); //return formatted date-time in asia/jakarta

This is UTC date; converting UTC to IST timezone; let sampleArray = [ { date: "2022-12-22T19:16:26.803" }, { date: "2022-12-22T19:16:26.77" }, { date: "2022-12-22T19:16:26.737" }, { date: "2022-12-22T19:16:26.72" } ]; // Get all the results whose boolresult is 'true' // solution 1 sampleArray.map((element) => { let utcDate = new Date(element.date).getTime(); let dateIST = new Date(utcDate); dateIST.setHours(dateIST.getHours() + 5); dateIST.setMinutes(dateIST.getMinutes() + 30); element.date = dateIST; }); console.log("Result ==>>", sampleArray); // solution 2 sampleArray.map((element) => { element.date = new Date(element.date).toLocaleString("en-US", { timeZone: "Asia/Kolkata" }); }); console.log("Result 2==>>", sampleArray);