1var d = new Date("2020-04-13T00:00:00.000+08:00"); /* midnight in China on April 13th */
2d.toLocaleString('en-US', { timeZone: 'America/New_York' });
3//=> "4/12/2020, 12:00:00 PM"
4// (midnight in China on April 13th is noon in New York on April 12th)
1function convertTZ(date, tzString) {
2 return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));
3}
4
5// usage: Asia/Jakarta is GMT+7
6convertTZ("2012/04/10 10:10:30 +0000", "Asia/Jakarta") // Tue Apr 10 2012 17:10:30 GMT+0700 (Western Indonesia Time)
7
8// Resulting value is regular Date() object
9const convertedDate = convertTZ("2012/04/10 10:10:30 +0000", "Asia/Jakarta")
10convertedDate.getHours(); // 17
11
12// Bonus: You can also put Date object to first arg
13const date = new Date()
14convertTZ(date, "Asia/Jakarta") // current date-time in jakarta.
1let s = "2005-07-08T11:22:33+0000";
2let d = new Date(Date.parse(s));
3
4// this logs for me
5// "Fri Jul 08 2005 13:22:33 GMT+0200 (Central European Summer Time)"
6// and something else for you
7
8console.log(d.toString())
9
10// this logs
11// Fri, 08 Jul 2005 11:22:33 GMT
12// for everyone
13
14console.log(d.toUTCString())
1/* @param {string} s - an ISO 8001 format date and time string
2** with all components, e.g. 2015-11-24T19:40:00
3** @returns {Date} - Date instance from parsing the string. May be NaN.
4*/
5function parseISOLocal(s) {
6 var b = s.split(/\D/);
7 return new Date(b[0], b[1]-1, b[2], b[3], b[4], b[5]);
8}
9
10document.write(parseISOLocal('2015-11-24T19:40:00'));