8

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.

Ben Hull
  • 7,524
  • 3
  • 36
  • 56

3 Answers3

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;
})()
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • Daylight savings wont indicate the hemisphere – AUSteve Oct 20 '11 at 07:03
  • 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. – Ben Hull Oct 21 '11 at 21:49
  • Thanks Kennebec - I'm going to accept your answer, because it's the kind of 'trick' I'm thinking of. – Ben Hull Oct 27 '11 at 02:48
  • 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. – Matt Johnson-Pint Sep 18 '15 at 16:47
4

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

Dave M
  • 1,302
  • 1
  • 16
  • 28
Robot Woods
  • 5,677
  • 2
  • 21
  • 30
  • Firefox uses the Google Location service which uses the IP address... Be aware that navigation.geolocation is not supported on all browsers. – AUSteve Oct 20 '11 at 07:06
  • I'll definitely use built in geolocation as my first preference - thanks! – Ben Hull Oct 21 '11 at 21:46
  • Thanks Robot - Your answer is probably the 'correct' one, but I can only accept one. – Ben Hull Oct 27 '11 at 02:49
2

@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
}
Terry Brown
  • 1,300
  • 14
  • 13