var d =newDate("2020-04-13T00:00:00.000+08:00");/* midnight in China on April 13th */
d.toLocaleString('en-US',{timeZone:'America/New_York'});//=> "4/12/2020, 12:00:00 PM"// (midnight in China on April 13th is noon in New York on April 12th)
// current datetime string in America/Chicago timezonelet chicago_datetime_str =newDate().toLocaleString("en-US",{timeZone:"America/Chicago"});// create new Date objectlet date_chicago =newDate(chicago_datetime_str);
functionchangeTimezone(date, ianatz){// suppose the date is 12:00 UTCvar invdate =newDate(date.toLocaleString('en-US',{timeZone: ianatz
}));// then invdate will be 07:00 in Toronto// and the diff is 5 hoursvar diff = date.getTime()- invdate.getTime();// so 12:00 in Toronto is 17:00 UTCreturnnewDate(date.getTime()- diff);// needs to substract}// E.g.var here =newDate();var there =changeTimezone(here,"America/Toronto");console.log(`Here: ${here.toString()}
Toronto: ${there.toString()}`);Run code snippetHide results