Is there an easy way to determine which hemisphere a user is in?
Asked Answered
G

3

9

I'm working on a project which includes seasonal content, and we're thinking of determining the user's location to work out what season it is for them. The obvious way of doing this is to Geo-locate their IP, then grab the latitude. > 0 is Northern hemisphere, and < 0 is Southern.

I'm happy to go that way - though it seems a bit of a waste to pinpoint an IP to an exact location, just to determine which half of the planet they're on - but I thought I'd throw it out there in case anyone has any tricks which might shortcut the process.

Request headers, stuff that can be extracted with JS on the client, it's all easy enough to get - I just don't think any of it helps.

Greeson answered 20/10, 2011 at 2:0 Comment(1)
I wanted to do something like this so that I could detect the user's spring/summer/fall/winter season. I used the DST trick combined with a compiled list of time zones that do not support DST. Heads up time zones that do not support DST tend to congregate around the equator where the time zone is observed on both sides of it and is of no help.Unbeknown
F
5

I'd first check the client's clock- if daylight savings exists in the client's calendar, you can tell if he is north or south of the equator.

if there is no dst information, you can use geolocation,

or ask the user if he is south of the equator...

window.whatHemisphere= (function(){
    var y= new Date().getFullYear();
    if(y.getTimezoneOffset()==undefined) return null;
    var jan= -(new Date(y, 0, 1, 0, 0, 0, 0).getTimezoneOffset()),
    jul= -(new Date(y, 6, 1, 0, 0, 0, 0).getTimezoneOffset()),
    diff= jan-jul;
    if(diff> 0) return 'N';
    if(diff< 0) return 'S'
    return null;
})()
Forme answered 20/10, 2011 at 6:37 Comment(4)
Daylight savings wont indicate the hemisphereSantoyo
This is a really neat trick - and I think daylight savings will indicate the hemisphere, as it tells us which half of the year summer occurs in... Most of the target audience will be in countries that do observe DST, so this is a worthy addition to the toolkit - thanks.Greeson
Thanks Kennebec - I'm going to accept your answer, because it's the kind of 'trick' I'm thinking of.Greeson
This is buggy, because y is just the year number here and doesn't have a getTimezoneOffset method. Also, the "if daylight saving exists" is a big IF, as much of the world does not do DST at all.Gnatcatcher
D
4

You could use the navigator.geolocation method to avoid any sort of IP service.

Doctrinal answered 20/10, 2011 at 2:5 Comment(3)
Firefox uses the Google Location service which uses the IP address... Be aware that navigation.geolocation is not supported on all browsers.Santoyo
I'll definitely use built in geolocation as my first preference - thanks!Greeson
Thanks Robot - Your answer is probably the 'correct' one, but I can only accept one.Greeson
K
3

@kennebec's answer is clever, but the code seems untested. Here's code that works. It doesn't help with the places that don't use daylight savings problem.

function whatHemisphere() {
    let y = new Date()
    if (y.getTimezoneOffset==undefined) return null
    y = y.getFullYear()
    let jan = -(new Date(y, 0, 1, 0, 0, 0, 0).getTimezoneOffset())
    let jul = -(new Date(y, 6, 1, 0, 0, 0, 0).getTimezoneOffset())
    let diff = jan - jul
    if (diff <  0) return 'N'
    if (diff >  0) return 'S'
    return null
}
Kepner answered 10/1, 2021 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.