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

它需要两个参数,

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

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

有什么简单的方法吗?


当前回答

我应该指出,我在可以使用的外部库方面受到了限制。moment.js和timezone-js不是我的选择。

我拥有的js日期对象是UTC。我需要在特定的时区(在我的例子中是“America/Chicago”)从这个日期获得日期和时间。

 var currentUtcTime = new Date(); // This is in UTC

 // Converts the UTC time to a locale specific format, including adjusting for timezone.
 var currentDateTimeCentralTimeZone = new Date(currentUtcTime.toLocaleString('en-US', { timeZone: 'America/Chicago' }));

 console.log('currentUtcTime: ' + currentUtcTime.toLocaleDateString());
 console.log('currentUtcTime Hour: ' + currentUtcTime.getHours());
 console.log('currentUtcTime Minute: ' + currentUtcTime.getMinutes());
 console.log('currentDateTimeCentralTimeZone: ' +        currentDateTimeCentralTimeZone.toLocaleDateString());
 console.log('currentDateTimeCentralTimeZone Hour: ' + currentDateTimeCentralTimeZone.getHours());
 console.log('currentDateTimeCentralTimeZone Minute: ' + currentDateTimeCentralTimeZone.getMinutes());

国际标准时间目前比“美国/芝加哥”早6小时。输出是:

currentUtcTime: 11/25/2016
currentUtcTime Hour: 16
currentUtcTime Minute: 15

currentDateTimeCentralTimeZone: 11/25/2016
currentDateTimeCentralTimeZone Hour: 10
currentDateTimeCentralTimeZone Minute: 15

其他回答

无耻地窃取自: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();
}

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

设置一个变量,用-符号分隔年、月和日,加上一个T和HH:mm:ss模式的时间,在字符串末尾加上+01:00(在我的例子中,时区是+1)。然后使用此字符串作为日期构造函数的参数。

// desired format: 2001-02-04T08:16:32+01:00
dateAndTime = year+"-"+month+"-"+day+"T"+hour+":"+minutes+":00+01:00";

var date = new Date(dateAndTime );

提供所需的时区,例如“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>

好了,找到了!

我使用的是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

我最近在Typescript中做了这个:

// fromTimezone example : Europe/Paris, toTimezone example: Europe/London
private calcTime( fromTimezone: string, toTimezone: string, dateFromTimezone: Date ): Date {
  const dateToGetOffset = new Date( 2018, 5, 1, 12 );

  const fromTimeString = dateToGetOffset.toLocaleTimeString( "en-UK", { timeZone: fromTimezone, hour12: false } );
  const toTimeString = dateToGetOffset.toLocaleTimeString( "en-UK", { timeZone: toTimezone, hour12: false } );

  const fromTimeHours: number = parseInt( fromTimeString.substr( 0, 2 ), 10 );
  const toTimeHours: number = parseInt( toTimeString.substr( 0, 2 ), 10 );

  const offset: number = fromTimeHours - toTimeHours;

  // convert to msec
  // add local time zone offset
  // get UTC time in msec
  const dateFromTimezoneUTC = Date.UTC( dateFromTimezone.getUTCFullYear(),
    dateFromTimezone.getUTCMonth(),
    dateFromTimezone.getUTCDate(),
    dateFromTimezone.getUTCHours(),
    dateFromTimezone.getUTCMinutes(),
    dateFromTimezone.getUTCSeconds(),
  );

  // create new Date object for different city
  // using supplied offset
  const dateUTC = new Date( dateFromTimezoneUTC + ( 3600000 * offset ) );

  // return time as a string
  return dateUTC;
}

我使用“en-UK”格式,因为它是一个简单的。可能是"en-US"之类的。

如果第一个参数是本地时区,第二个参数是目标时区,它将返回一个具有正确偏移量的Date对象。