July 11, 2000 - Local Times Around the World | WebReference

July 11, 2000 - Local Times Around the World

Yehuda Shiran July 11, 2000
Local Times Around the World
Tips: July 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

First thing you need to do is gather the local times in the cities you are interested in, wrt GMT. Here is the list we use in our example:

var London = 1;
var Moscow = 3;
var NewDelhi = 5.5; 
var Tokyo = 9;
var LA = -7;
var NewYork = -4;
var Beijing = 8;
var WashingtonDC = -4;

Next, we create the Date object:

now = new Date();

and then the GMT:

var ar1 = now.toGMTString().split(" ");

Since we need to manipulate the hour part of the GMT, we split the string and store its parts in ar1. We then take the time part and split it into ar2. The hour part is the first element of the array ar2:

var ar2 = ar1[4].split(":");

Now, we are ready to adjust the GMT hour for the Daylight Saving Time difference, denoted as GMT:

var GMThour = parseInt(ar2[0]) + GMT;

For each city, we crate a current Date object:

now = new Date();

set the hours according the GMT and the given offset:

now.setHours(GMThour + Moscow);

and then write the date according to the local format definitions:

document.write("Moscow:  " + now.toLocaleString()); document.write("<BR>");

Press here to see the local time in cities around the world.