Convert date to another timezone in JavaScript

后端 未结 24 3041
没有蜡笔的小新
没有蜡笔的小新 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:32

    You can try this also for convert date timezone to India:

    var indianTimeZoneVal = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'});
    var indainDateObj = new Date(indianTimeZoneVal);
    indainDateObj.setHours(indainDateObj.getHours() + 5);
    indainDateObj.setMinutes(indainDateObj.getMinutes() + 30);
    console.log(indainDateObj);
    
    0 讨论(0)
  • 2020-11-21 05:32

    You can also use https://www.npmjs.com/package/ctoc_timezone

    It has got much simple implementation and format customisation.

    Changing format in toTimeZone:

    CtoC.toTimeZone(new Date(),"EST","Do MMM YYYY hh:mm:ss #{EST}");

    Output :

    28th Feb 2013 19:00:00 EST

    You can explore multiple functionalities in the doc.

    0 讨论(0)
  • 2020-11-21 05:35

    Just set your desire country timezone and You can easily show in html it update using SetInteval() function after every one minut. function formatAMPM() manage 12 hour format and AM/PM time display.

    $(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;
            }
    
    
        });
    
    0 讨论(0)
  • 2020-11-21 05:36

    Okay, found it!

    I'm using timezone-js. this is the code:

    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
    
    0 讨论(0)
  • 2020-11-21 05:37

    Here is my code, it is working perfectly, you can try with give below demo:

    $(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>

    0 讨论(0)
  • 2020-11-21 05:38

    I recently did this in 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;
    }
    

    I Use "en-UK" format because it is a simple one. Could have been "en-US" or whatever works.

    If first argument is your locale timezone and seconde is your target timezone it returns a Date object with the correct offset.

    0 讨论(0)
提交回复
热议问题