Convert date to another timezone in JavaScript

Tags: ,



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

var aestTime = new Date().toLocaleString("en-US", {timeZone: "Australia/Brisbane"});
console.log('AEST time: '+ (new Date(aestTime)).toISOString())

var asiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Shanghai"});
console.log('Asia time: '+ (new Date(asiaTime)).toISOString())

var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
console.log('USA time: '+ (new Date(usaTime)).toISOString())

var indiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
console.log('India time: '+ (new Date(indiaTime)).toISOString())

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString



Source: stackoverflow