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

它需要两个参数,

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

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

有什么简单的方法吗?


当前回答

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

其他回答

我应该指出,我在可以使用的外部库方面受到了限制。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

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

这是为我工作在React本机应用程序。

import moment from 'moment-timezone'

function convertTZ(date, tzString) {
  const formatedDate = moment(date).tz(tzString).format()
  return formatedDate
}

export {convertTZ}

所有这些答案都有点多余,但这对我来说是有效的,可以获得具有特定小时偏移量的当前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));

只需设置你想要的国家时区,你可以很容易地在html中显示它更新使用setinterval()函数后每一分钟。函数formatAMPM()管理12小时格式和AM/PM时间显示。

$(document).ready(function(){
        var pakTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Karachi"});
        pakTime = new Date(pakTime);

        var libyaTime = new Date().toLocaleString("en-US", {timeZone: "Africa/Tripoli"});
        libyaTime = new Date(libyaTime);



         document.getElementById("pak").innerHTML = "PAK  "+formatAMPM(pakTime);
         document.getElementById("ly").innerHTML = "LY   " +formatAMPM(libyaTime);

        setInterval(function(today) {
            var pakTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Karachi"});
            pakTime = new Date(pakTime);

            var libyaTime = new Date().toLocaleString("en-US", {timeZone: "Africa/Tripoli"});
            libyaTime = new Date(libyaTime);


           document.getElementById("pak").innerHTML = "PAK  "+formatAMPM(pakTime);
           document.getElementById("ly").innerHTML = "LY  " +formatAMPM(libyaTime);

        },10000);

         function formatAMPM(date) {
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var ampm = hours >= 12 ? 'pm' : 'am';
            hours = hours % 12;
            hours = hours ? hours : 12; // the hour '0' should be '12'
            minutes = minutes < 10 ? '0'+minutes : minutes;
            var strTime = hours + ':' + minutes + ' ' + ampm;
            return strTime;
        }


    });