Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Last active July 29, 2016 20:32
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 juandopazo/b52820e368739ed19cb206e3f3893166 to your computer and use it in GitHub Desktop.
Save juandopazo/b52820e368739ed19cb206e3f3893166 to your computer and use it in GitHub Desktop.
export function getTimezoneOffset(date, timeZone) {
const parts = new Intl.DateTimeFormat('en-US', {
timeZone,
hour12: false,
year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric'
}).format(date).split(/[\/\s:]/);
// The year string contains a comma, like "2016,", so we strip it using parseInt
const year = parseInt(parts[2], 10);
// The month is rendered in human readable terms, which are betweeen 1-12, but the API requires 0-11
const month = parts[0] - 1;
const day = parts[1];
const hour = parts[3];
const minute = parts[4];
const second = parts[5];
const t1 = Date.UTC(year, month, day, hour, minute, second);
const t2 = new Date(date).setMilliseconds(0);
return (t1 - t2) / 60000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment