Agent Zebra Agent Zebra - 1 year ago 144
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

Answer Source

It is very simple.

var offset = - new Date().getTimezoneOffset();
document.write((offset > 0 ? '+' : '') + offset / 60);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download