Convert date to another timezone in JavaScript

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

    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.

提交回复
热议问题