-1

I'd like to be able to get the timezone that the client browser is running in, so I can compare it to the user's home timezone, which I know via a config option like "America/Vancouver". I'm using moment.js and moment-timezone.js, and I can't figure out how to do that!

Update: I don't really need the client's timezone to get the functionality I want - I just need to know if it's different than the one I have specified as the user's in my config option. I have found a solution to this problem, which doesn't require loading an entirely new library, below.

(Question updated to make it more clear.)

Joe Orost
  • 878
  • 7
  • 12
  • 1
    possible duplicate of [Getting the client's timezone in JavaScript](http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript) – nothingisnecessary Oct 28 '14 at 20:00
  • But the user is the client.. so I think you are trying to compare the same thing.. no? – Morten OC Oct 28 '14 at 20:02
  • 1
    There's an [open issue](https://github.com/moment/moment-timezone/issues/138) for getting the user's current timezone, so I don't think it's currently possible with momentjs. I would look at the proposed duplicate--I've had success with jstz. – Andrew Whitaker Oct 29 '14 at 02:10
  • Thanks Andrew! I was able to find a working solution, which I posted below, without having to load in yet another library (e.g. jstz). Note, I don't need to know the client browser timezone, I just need to know if it's different than the one specified by the user. – Joe Orost Oct 29 '14 at 16:07

3 Answers3

0

You don't need moment to do this, it is a native javascript function:

var offset = new Date().getTimezoneOffset();

Then you just compare the offset from client (the "client timezone") to the offset from server (the "user timezone", assuming stored in a DB or profile somehow) and apply the difference if adjusting datetime values in the presentation layer (for example; you would also combine this with localizing the date display to the client's/user's culture typically).

Example:

If your client TZ offset is -480 (-8 GMT, AKA "PST") and your server TZ offset is -8, they cancel each other out when displaying adjusted timezone-relevant data from server. Note that by "server timezone" this could also refer to the known user's timezone in your application, then just compare the timezone offset from moment for that region to the client TZ offset.

Fiddle using native JS: http://jsfiddle.net/34qfwf1y/1/

Code:

// client TZ (offset from GMT)
var clientTZOffset = new Date().getTimezoneOffset() * -1;

// user home TZ from server (faked here; pretend you got it from server and passed it to js)
// pretend user's home is in Los Angeles (using Pacific Daylight Time since it is 28 October 2014 today)
var User = {
    Profile: {
        TimeZoneOffset: -420
    }
}; // server is GMT -7

var origDate = new Date();
var diff = User.Profile.TimeZoneOffset - clientTZOffset;
var clientTZHoursFromGMT = clientTZOffset / 60;
var homeTZHoursFromGMT = User.Profile.TimeZoneOffset / 60;
var adjDate = new Date(origDate.getTime() + diff*60000);

document.getElementById('isHomeTZ').innerHTML = (clientTZHoursFromGMT===homeTZHoursFromGMT).toString().toUpperCase();
document.getElementById('originalDate').innerHTML = origDate.toString();
document.getElementById('adjustedDate').innerHTML = adjDate.toString();
document.getElementById('homeTZ').innerHTML = homeTZHoursFromGMT.toString();
document.getElementById('clientTZ').innerHTML = clientTZHoursFromGMT.toString();
Client is in user's home time zone (PDT):
<div id="isHomeTZ"></div>
<br/>Original date (ignore TZ display, pretend it is PDT / Los Angeles):
<div id="originalDate"></div>
<br/>Saved User's Home TZ (simulated; in hours from GMT)
<div id="homeTZ"></div>
<br/>Client TZ (in hours from GMT)
<div id="clientTZ"></div>
<br/>Adjusted date (in client timezone)
<div id="adjustedDate"></div>
nothingisnecessary
  • 6,099
  • 36
  • 60
  • I'm sitting here in EDT and your code is alerting TRUE. Also, thanks but the time on my web server isn't relevant. – Joe Orost Oct 29 '14 at 01:26
  • it's not about time on web server, it's about concept that user's "home" time could have come from web server if saved in user's profile -- but you didn't specify where user's home TZ came from in your question, so I was just filling in the blanks with a common way to do this. – nothingisnecessary Oct 29 '14 at 13:14
  • Thanks for trying. I couldn't get your code to work, and I figured out how I could do it - I posted an answer and implemented it in my WebApp. Thanks again! – Joe Orost Oct 31 '14 at 04:36
0

I'm not sure what the difference would be between the "client's browser" and the "user's timezone"?

You can get the current user's timezone offset from GMT in minutes using:

moment().zone();

moment.js docs Timezone Offset

Dave Cripps
  • 929
  • 7
  • 11
  • Sorry, my "user's timezone" is a fixed timezone where he lives, e.g. `America/Vancouver`. "Client browser timezone" means the timezone where the client browser is running. Think of a world traveler. I want to know if he's away from his home timezone. – Joe Orost Oct 28 '14 at 20:23
0

I woke up with an answer that works:

function awayFromHomeTZ(tz) {
    var there = moment.tz(tz);
    var here = moment();
    var thereHHMM = there.hour() * 60 + there.minute();
    var hereHHMM = here.hour() * 60 + here.minute();
    return Math.abs(thereHHMM - hereHHMM) > 59;
}

Fiddle: http://jsfiddle.net/0418th02/4/

Joe Orost
  • 878
  • 7
  • 12