xxxxxxxxxx
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timezone);
xxxxxxxxxx
var d = new Date("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)
xxxxxxxxxx
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
Run code snippet
xxxxxxxxxx
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(browserTimezone);
xxxxxxxxxx
// current datetime string in America/Chicago timezone
let chicago_datetime_str = new Date().toLocaleString("en-US", { timeZone: "America/Chicago" });
// create new Date object
let date_chicago = new Date(chicago_datetime_str);
xxxxxxxxxx
getGMT() {
let gmtFigure = new Date().getTimezoneOffset() / -60;
let gmtSign = gmtFigure >= 0 ? '+' : '-';
let gmtlength = this.digits_count(gmtFigure);
let gmt = 'GMT';
if (gmtlength == 1) {
gmt += gmtSign + '0' + Math.abs(gmtFigure);
} else if (gmtlength > 1) {
gmt += gmtSign + Math.abs(gmtFigure);
}
return gmt;
}
digits_count(n) {
var count = 0;
n = Math.abs(n);
if (n >= 1) ++count;
while (n / 10 >= 1) {
n /= 10;
++count;
}
return count;
}
xxxxxxxxxx
function changeTimezone(date, ianatz) {
// suppose the date is 12:00 UTC
var invdate = new Date(date.toLocaleString('en-US', {
timeZone: ianatz
}));
// then invdate will be 07:00 in Toronto
// and the diff is 5 hours
var diff = date.getTime() - invdate.getTime();
// so 12:00 in Toronto is 17:00 UTC
return new Date(date.getTime() - diff); // needs to substract
}
// E.g.
var here = new Date();
var there = changeTimezone(here, "America/Toronto");
console.log(`Here: ${here.toString()}\nToronto: ${there.toString()}`);
Run code snippetHide results
xxxxxxxxxx
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timezone);
// Africa/Casablanca