Answers for "js get timezone of a time"

4

js get timezone name

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
Posted by: Guest on October-31-2020
0

javascript get specific timezone

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
Posted by: Guest on November-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language