Created
November 11, 2019 11:56
-
-
Save cosbgn/f18af53f223092f659818bd565194825 to your computer and use it in GitHub Desktop.
Find if user is in the EU without any extra API or server-side code or IP Address
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
// You can easily find out if the user is in the EU calculating the offset from GMT. | |
// It's not a perfect method but for simple use cases it works perfectly | |
// It also catches all Africa | |
const offset = new Date().getTimezoneOffset(); | |
// 0 is GMT and - 120 is Finland - | |
// It should catch all the EU - However it also catches most of Africa so use it carefully! | |
let inEu = false | |
if (1 > offset && offset > -130 ){ | |
inEu = true | |
console.log("The user is in the European Union!") | |
} | |
// Alternatively you could check the timezone | |
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone | |
const continentCity = timezone.split("/") | |
const continent = continentCity[0] | |
const city = continentCity[1] | |
console.log("the user is in ", continent, ". The city is: ", city) | |
if (continent == 'Europe') { | |
inEu = true | |
console.log("The user is in the European Union!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Probably the best and easiest way to check if someone is in Europe, is the following:
const inEu = Intl.DateTimeFormat().resolvedOptions().timeZone.startsWith("Europe")? true : false