Skip to content

Instantly share code, notes, and snippets.

@Robokishan
Last active August 26, 2021 09:08
Show Gist options
  • Save Robokishan/2ac0378f4c391d617835b06b08782dcc to your computer and use it in GitHub Desktop.
Save Robokishan/2ac0378f4c391d617835b06b08782dcc to your computer and use it in GitHub Desktop.
Just a time zone hack
let date = new Date();
let offset = 19800;
console.log(parseInt(date.getTime() / 1000));
console.log(date.getTimezoneOffset());
console.log(date.toISOString());
let _offset = 28800 * 1000
console.log(_offset);
date.setTime( date.getTime() - (offset * 1000) + _offset );
console.log(date.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }), _offset,date.getTimezoneOffset(), date.getTimezoneOffset() * 60 * 1000);
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
}
var here = new Date();
var there = changeTimezone(here, "America/Toronto");
console.log(`Here: ${here.toString()}\nToronto: ${there.toLocaleString()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment