For example, the difference in Eastern and Central is 1. My solution below feels hacky. Is there a easier / better way?
var diff = (parseInt(moment().tz("America/New_York").format("ZZ")) - parseInt(moment().tz("America/Chicago").format("ZZ"))) / 100;
My example is using the Momentjs library.
It's impossible to calculate the difference between two arbitrary time zones. You can only calculate a difference for a specific moment in time.
- It's currently 4 hours difference between London and New York (writing this on Mar 25, 2015).
- But it was 5 hours difference a few weeks ago, and it will be 5 a few weeks from now.
- Each time zone switches offsets for daylight saving time at a different point in time.
This is true in the general case between two time zones. However some time zones either switch exactly at the same time, or don't switch at all.
- There is always one hour between London and Paris, because they switch at the same moment in time.
- There are always 3 hours between Arizona and Hawaii, because neither have DST.
Keep in mind that in the United States, each time zone that uses DST actually switches at a different moment in time. They all switch at 2:00 AM in their local time, but not at the same universal moment in time.
- So between Chicago and New York, there is usually 1 hour apart
- But for two brief periods each year they are either 2 hours apart, or have the same exact time.
See also "Time Zone != Offset" in the timezone tag wiki.
Now with regard to moment-timezone, you said in comments:
The web server is in the Eastern time zone; we're in Central. I need the difference in the user's timezone and the server.
The time zone of the web server is irrelevant. You should be able to host from anywhere in the world without affecting your application. If you can't, then you're doing it wrong.
You can get the current time difference between your time zone (US Central time) and the user's. You don't even need to know the user's exact time zone for this, if the code is running in the browser:
var now = moment();
var localOffset = now.utcOffset();
now.tz("America/Chicago"); // your time zone, not necessarily the server's
var centralOffset = now.utcOffset();
var diffInMinutes = localOffset - centralOffset;
If instead the code was running on the server (in a node.js app), then you would need to know the user's time zone. Just change the first line like this:
var now = moment.tz("America/New_York"); // their time zone