Convert date to another timezone in JavaScript

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

    Stolen shamelessly from: 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();
    
        // convert to msec
        // add local time zone offset
        // get UTC time in msec
        var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    
        // 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();
    }
    

    this function is useful to calculate time zone value by providing name of a city/country and offset value

提交回复
热议问题