Home >Web Front-end >JS Tutorial >JavaScript script to convert local time to other time zones_time and date
But what if you want to display the time in a different region—for example, if your headquarters is in another country and you want to view the "home" time instead of the local time?
To do this, various time calculations must be performed to convert local time to destination time. This article explains how to perform these calculations.
Step One:
The first step in things is to get the local time. In JavaScript, this can certainly be done easily by initializing a Data() object.
// create Date object for current location
d = new Date();
By calling the getTime() method of the Data() object, the time between January 1, 1970 and now can be displayed The number of milliseconds between.
// convert to msec since Jan 1 1970
localTime = d.getTime();
Step 2:
Next step, find out the local time through the getTimezoneOffset() method of the Data() object Time offset value. By default, this method displays the time zone offset value result in minutes, so this value is converted to milliseconds in the earlier calculation.
// obtain local UTC offset and convert to msec
localOffset = d.getTimezoneOffset() * 60000;
Note that the negative return value of the getTimezoneOffset() method indicates that the local time is before Universal Standard Time (UTC) , while a positive return value indicates that the local time is behind Coordinated Universal Time (UTC).
NOTE: In case you're wondering how I got the multiplication factor of 60,000, remember that 1,000 milliseconds equals one second, and one minute equals 60 seconds. Therefore, to convert minutes to milliseconds, multiply 60 by 1000 which equals 60000.
Step 3
Add the local time and the local time zone offset to get the current international standard time (UTC).
// obtain UTC time in msec
utc = localTime localOffset;
Here, the variable utc contains the current international standard time (UTC). However, this time is expressed as the number of milliseconds from January 1, 1970 to the present. Let it be expressed this way for now, since there are still some calculations to do.
Step 4
After obtaining the International Standard Time (UTC), obtain the hourly offset value of the International Standard Time (UTC) of the target city, convert it into milliseconds, and add the International Standard Time (UTC).
// obtain and add destination's UTC time offset
// for example, Bombay
// which is UTC 5.5 hours
offset = 5.5;
bombay = utc (3600000*offset);
NOTE: In case you're wondering how I got the multiplication factor of 3,600,000, remember that 1000 milliseconds equals one second, and one hour equals 3600 seconds. Therefore, to convert hours to milliseconds, multiply 3600 by 1000 which equals 3600000.
At this time, the variable bombay contains the local time in Mumbai, India. This local time is expressed as the number of milliseconds from January 1, 1970 to the present. Obviously, this doesn't make sense, so we have to do some calculations.
Step 5
By initializing a new Data() object and calling the toLocalString() method of this object, we convert the time value calculated in the previous step into a date that everyone can understand/ Time string.
// convert msec value to date string
nd = new Date(bombay);
document.writeln("Bombay time is " nd.toLocaleString() "
");
This way The conversion is complete!
Summary
After understanding the above steps, let's take another look at this script (Listing A), which creates a compact, custom function calcTime() to perform all calculations and return a time value.
List A