Convert date to another timezone in JavaScript

后端 未结 24 3031
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 04:45

I am looking for a function to convert date in one timezone to another.

It need two parameters,

  • date (in format \"2012/04/10 10:10:30 +0000\")
24条回答
  •  灰色年华
    2020-11-21 05:26

    Provide the desired time zone, for example "Asia/Tehran" to change the current time to that timezone. I used "Asia/Seoul".

    You can use the following codes. change the style if you need to do so.

    please keep in mind that if you want to have h:m:s format instead of HH:MM:SS, you'll have to remove "function 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;}
    }

    This Pen was originally developed for KOCOWAFA.com

    (Seoul, Korea)

提交回复
热议问题