Rizky Ramadhan Rizky Ramadhan - 2 months ago 11
Javascript Question

Convert date to another timezone in JavaScript

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")

  • timezone string ("Asia/Jakarta")



The timezone string is described in http://en.wikipedia.org/wiki/Zone.tab

Is there an easy way to do this?

Answer

For moment.js users, you can now use moment-timezone. Using it, your function would look something like this:

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}