Skip to content

Instantly share code, notes, and snippets.

@kawanet
Created November 30, 2022 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawanet/3bfa725e28f628b1962c9a41a1d74930 to your computer and use it in GitHub Desktop.
Save kawanet/3bfa725e28f628b1962c9a41a1d74930 to your computer and use it in GitHub Desktop.
const getTimezoneOffset = (timeZone, dt) => new Intl.DateTimeFormat("en-US", {timeZoneName: "longOffset", timeZone}).formatToParts(dt).find(v => v.type === "timeZoneName")?.value;
getTimezoneOffset("America/New_York", new Date()); // => "GMT-05:00"
getTimezoneOffset("America/New_York"); // => "GMT-05:00"
getTimezoneOffset("invalid"); // => Uncaught RangeError: Invalid time zone specified:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment