Agent Zebra Agent Zebra - 10 months ago 53
Javascript Question

Getting UTC timezone in the format UTC +/-

I want to get the users timezone in relation to UTC and then display it UTC +/- . for example a California user should show UTC -8 (or -7 depending), while Bahrain would show UTC +3, etc.

The following code doesn't tell me if it's plus or minus tho, it just gives the answer 7 at the moment. How do I get it to give me a plus or minus sign?

var offset = new Date().getTimezoneOffset();
document.write(offset / 60);

Here's a fiddle of the above


It is very simple.

var offset = - new Date().getTimezoneOffset();
document.write((offset > 0 ? '+' : '') + offset / 60);