Convert date to another timezone in JavaScript

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

    Set a variable with year, month, and day separated with '-' symbols, plus a 'T' and the time in HH:mm:ss pattern, followed by +01:00 at the end of the string (in my case the time zone is +1). Then use this string as the argument for the date constructor.

    //desired format: 2001-02-04T08:16:32+01:00
    dateAndTime = year+"-"+month+"-"+day+"T"+hour+":"+minutes+":00+01:00";
    
    var date = new Date(dateAndTime );
    
    0 讨论(0)
  • 2020-11-21 05:39

    I don't know an easy method to convert a date object to any time zone, but if you want to convert it to the local time zone, you can just convert it with Date.prototype.getTime() to the corresponding number of milliseconds, and back again.

    date = new Date('2016-05-24T13:07:20');
    date = new Date(date.getTime());
    

    For example, date.getHours() will now return 15 instead of 13 if you are, like me, in Austria (and it's summer).

    I've read that the various datetime functions may exhibit non-standard behaviour in some browsers, so test this first. I can confirm that it works in Chrome.

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

    If you just need to convert timezones I have uploaded a stripped-down version of moment-timezone with just the bare minimum functionallity. Its ~1KB + data:

    S.loadData({
        "zones": [
            "Europe/Paris|CET CEST|-10 -20|01010101010101010101010|1GNB0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0|11e6",
            "Australia/Sydney|AEDT AEST|-b0 -a0|01010101010101010101010|1GQg0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0|40e5",
        ],
        "links": [
            "Europe/Paris|Europe/Madrid",
        ]
    });
    
    let d = new Date();
    console.log(S.tz(d, "Europe/Madrid").toLocaleString());
    console.log(S.tz(d, "Australia/Sydney").toLocaleString());
    
    0 讨论(0)
  • 2020-11-21 05:43

    I was having trouble using Moment Timezone. I am adding this answer just so if somebody else faces the same issue. So I have a date string 2018-06-14 13:51:00 coming from my API. I know that this is stored in UTC but the string doesn't speak for itself.

    I let moment timezone know, what timezone this date is from by doing:

    let uTCDatetime = momentTz.tz("2018-06-14 13:51:00", "UTC").format();
    // If your datetime is from any other timezone then add that instead of "UTC"
    // this actually makes the date as : 2018-06-14T13:51:00Z
    

    Now I would like to convert it to a specific timezone by doing:

    let dateInMyTimeZone = momentTz.tz(uTCDatetime, "Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss");
    // now this results into: 2018-06-14 19:21:00, which is the corresponding date in my timezone.
    
    0 讨论(0)
  • 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

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

    You can use to toLocaleString() method for setting the timezone.

    new Date().toLocaleString('en-US', { timeZone: 'Indian/Christmas' })
    

    For India you can use "Indian/Christmas" and the following are the various timeZones,

    "Antarctica/Davis",
        "Asia/Bangkok",
        "Asia/Hovd",
        "Asia/Jakarta",
        "Asia/Phnom_Penh",
        "Asia/Pontianak",
        "Asia/Saigon",
        "Asia/Vientiane",
        "Etc/GMT-7",
        "Indian/Christmas"
    
    0 讨论(0)
提交回复
热议问题