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

它需要两个参数,

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

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

有什么简单的方法吗?


当前回答

这是我的代码,它工作得很完美,你可以试试下面的演示:

$(document).ready(function() { //EST setInterval( function() { var estTime = new Date(); var currentDateTimeCentralTimeZone = new Date(estTime.toLocaleString('en-US', { timeZone: 'America/Chicago' })); var seconds = currentDateTimeCentralTimeZone.getSeconds(); var minutes = currentDateTimeCentralTimeZone.getMinutes(); var hours = currentDateTimeCentralTimeZone.getHours()+1;//new Date().getHours(); var am_pm = currentDateTimeCentralTimeZone.getHours() >= 12 ? "PM" : "AM"; if (hours < 10){ hours = "0" + hours; } if (minutes < 10){ minutes = "0" + minutes; } if (seconds < 10){ seconds = "0" + seconds; } var mid='PM'; if(hours==0){ //At 00 hours we need to show 12 am hours=12; } else if(hours>12) { hours=hours%12; mid='AM'; } var x3 = hours+':'+minutes+':'+seconds +' '+am_pm // Add a leading zero to seconds value $("#sec").html(x3); },1000); }); <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <p class="date_time"><strong id="sec"></strong></p> </body> </html>

其他回答

有一个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

希望对大家有用

我在使用瞬间时区时遇到了麻烦。我加上这个答案是为了让其他人面对同样的问题。所以我有一个日期字符串2018-06-14 13:51:00来自我的API。我知道这是存储在UTC,但字符串本身并不说话。

我让moment timezone知道这个日期来自哪个时区:

let uTCDatetime = momentTz.tz("2018-06-14 13:51:00", "UTC").format();
// If your datetime is from any other timezone then add that instead of "UTC"
// this actually makes the date as : 2018-06-14T13:51:00Z

现在我想通过这样做将其转换为特定的时区:

let dateInMyTimeZone = momentTz.tz(uTCDatetime, "Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss");
// now this results into: 2018-06-14 19:21:00, which is the corresponding date in my timezone.

提供所需的时区,例如“Asia/德黑兰”,以将当前时间更改为该时区。我用了“亚洲/首尔”。

您可以使用以下代码。如果需要更改样式。

请记住,如果你想要h:m:s格式而不是HH:MM:SS,你必须删除“函数kcwcheckT(i)”。

function kcwcheckT(i) { if (i < 10) { i = "0" + i; } return i; } function kcwt() { var d = new Date().toLocaleString("en-US", {timeZone: "Asia/Seoul"}); d = new Date(d); var h = d.getHours(); var m = d.getMinutes(); var s = d.getSeconds(); h = kcwcheckT(h); m = kcwcheckT(m); s = kcwcheckT(s); document.getElementById("kcwcurtime").innerHTML = h + ":" + m + ":" + s; var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; document.getElementById("kcwcurday").innerHTML = days[d.getDay()] } kcwt(); window.setInterval(kcwt, 1000); @import url('https://fonts.googleapis.com/css2?family=Nunito&display=swap'); .kcwsource {color:#040505;cursor: pointer;display:block;width: 100%;border: none;border-radius:5px;text-align:center;padding: 5px 10px 5px 10px;} .kcwsource p {font-family: 'Nunito', sans-serif;} .CurTbx {color:#040505;cursor: pointer;display:block;width: 100%;border: none;border-radius:5px;text-align:center;padding: 5px 10px 5px 10px;} .kcwcstyle {font-family: 'Nunito', sans-serif; font-size: 22px;display: inline-block;} .kcwcurstinf {font-family: 'Nunito', sans-serif; font-size: 18px;display: inline-block;margin: 0;} .kcwcurday {margin: 0;} .kcwcurst {margin: 0 10px 0 5px;} /*Using the css below you can make your style responsive!*/ @media (max-width: 600px){ .kcwcstyle {font-size: 14px;} .kcwcurstinf {font-size: 12px;} } <div class="kcwsource"><p>This Pen was originally developed for <a href="http://kocowafa.com" target="_blank">KOCOWAFA.com</a></p></div> <div class="CurTbx"><p class="kcwcurst kcwcstyle" id="kcwcurday"></p><p class="kcwcurst kcwcstyle" id="kcwcurtime"></p><p class="kcwcurstinf">(Seoul, Korea)</p></div>

如果你有一个时区偏移,你可以使用这个:

const timezoneDate = (timestamp: number, offsetInHours: number) => {
  const shiftedTimestam = timestamp + offsetInHours * 3600000;

  const shiftedDate = new Date(shiftedTimestam);

  return {
    date: shiftedDate.getUTCDate(),
    hours: shiftedDate.getUTCHours(),
    minutes: shiftedDate.getUTCMinutes(),
    // ...other UTC methods
  }
};

// Usage:
const newYorkDate = timezoneDate(Date.now(), -5);

console.log(
  `It is ${newYorkDate.hours}:${newYorkDate.minutes} in NYC`
);

你可以使用to toLocaleString()方法来设置时区。

new Date().toLocaleString('en-US', { timeZone: 'Indian/Christmas' })

对于印度,你可以使用“印度/圣诞节”,以下是不同的时区,

"Antarctica/Davis",
    "Asia/Bangkok",
    "Asia/Hovd",
    "Asia/Jakarta",
    "Asia/Phnom_Penh",
    "Asia/Pontianak",
    "Asia/Saigon",
    "Asia/Vientiane",
    "Etc/GMT-7",
    "Indian/Christmas"