1

How do I use a different time zone time in a JavaScript?

I already know how to get the current system time but I am confused how to display different time zone time. This one is displaying as an alert, but I need it as a usual displayer not as an alert.

function calcTime(city, offset) {
    d = new Date();

    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    nd = new Date(utc + (3600000*offset));

    return "The local time in " + city + " is " + nd.toLocaleString();
}

alert(calcTime('U.S.A', '-5.0'));
DA.
  • 39,848
  • 49
  • 150
  • 213
  • possible duplicate of [How to initialize javascript date to a particular timezone](http://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone) – Matt Johnson-Pint Mar 22 '14 at 21:51

1 Answers1

0

The "time" is absolute.

How you display the time (including time zone) is representational.

So the key thing to understand is that you DON'T want to modify the time VALUE - you just need to alter your FORMAT statement to use one or another time zone.

For example, this uses the Moment.js timezone plugin:

moment().tz("America/Los_Angeles").format("h:mm a")

Here is a great reference:

Stack Overflow 'Timezone' tag wiki

Community
  • 1
  • 1
FoggyDay
  • 11,962
  • 4
  • 34
  • 48