EndenDragon EndenDragon - 7 months ago 11
Javascript Question

How do I get the current timezone offset (and formatted it properly)?

How would I use javascript to format the computer's timezone offset to this format:

<+/-><hourDiff>:<minuteDiff>
, as in
-7:00
(America/Los_Angeles) for example.

Answer

getTimezoneOffset returns a Number representing the time difference between UTC and Local Time, in minutes

var date = new Date();
var tzOffset = date.getTimezoneOffset();
var hours = ~~(tzOffset / 60);
var minutes = Math.abs(tzOffset % 60);
alert('Hours: ' + hours + ' Minutes: ' + minutes);

Comments