Last active
July 29, 2016 20:32
-
-
Save juandopazo/b52820e368739ed19cb206e3f3893166 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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