Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cosbgn/f18af53f223092f659818bd565194825 to your computer and use it in GitHub Desktop.
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
// 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!")
}
@cosbgn
Copy link
Author

cosbgn commented Nov 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment