JavaScript scripting with local conversion into other time zones
Undoubtedly, the script can use JavaScript users through direct Show clock conveniently displayed on the web site, local time. However, if you want to show that different regions of the time - for example, if your headquarters in other countries, do you want to see the "national" rather than hours local time, then how do?
To achieve this, the need for various local time will be calculated in the time spent for the purpose of conversion time. This paper will explain how to proceed with the calculation.
Step 1:
The first step is to obtain the things local time. In JavaScript, which undoubtedly can initialize a Data () object to easily completed.
/ / Create Date object for current location
D = new Date ();
Data by calling () object getTime () method, which shows that in January 1970 after 1 this time was between a few milliseconds.
/ / Convert to msec since Jan 1 1970
LocalTime = d.getTime ();
Step 2:
Next, through Data () object getTimezoneOffset () method to identify local time offset value. By default, the minutes show that this method to time zone offsets of the results, in the earlier calculation of this value should be converted into milliseconds.
/ / Obtain local UTC offset and convert to msec
LocalOffset = d.getTimezoneOffset () * 60000;
Note that getTimezoneOffset () method returns a negative local time in the world that Standard Time (UTC), and the value that is returned to local time in the global Standard Time (UTC) after.
Note: if you want to know how I get this multiplier factor of 60,000, equivalent to one second millisecond remember 1000, and equivalent to one minute 60 seconds. Therefore, the minutes will be converted into milliseconds, use 60 times the 1000 equivalent to 60000.
The third step
Local time and local time zone offset by the sum of the current international Standard Time (UTC).
/ / Obtain UTC time in msec
Utc = localTime localOffset;
Here, variable utc contain the current international standard time (UTC). However, this time to January 1, 1970 to now by the presence of a few milliseconds to that. So that it temporarily, but also because of some calculation.
Step 4
By the international standard time (UTC), and then obtain goals at the international standard time (UTC) hour shift value, put it into milliseconds, together with 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: if you want to know how I get this multiplier factor of 3.6 million, equivalent to one second to remember 1000 ms, and one hour is 3,600 seconds. Therefore, the hours will be converted into milliseconds, use 3600 multiplied by the 1000 equivalent 3600000.
At this juncture, variable bombay includes local time the city of Mumbai, India. This local time to January 1, 1970 to now by the presence of a few milliseconds to that. Obviously, this is not very reasonable, and therefore we have to carry out some calculation.
Step 5
Through the initialization of a new Data () object, and call this object toLocalString () method, we will be the first step in the calculation of the time value can be converted into one we can read the date / time strings.
/ / Convert msec value to date string
Nd = new Date (bombay);
Document.writeln ( "Bombay time is" nd.toLocaleString () "<br>");
This conversion is complete!
Aggregate
Understanding of the above steps, we will take a look at this script (list A), which established a compact, custom function calcTime () for the implementation of all the calculation and return to a time value.
A List
<html>
<head>
<script Language="JavaScript">
/ / Function to calculate local time
/ / In a different city
/ / Given the city's UTC offset
Function calcTime (city, offset) (
/ / Create Date object for current location
D = new Date ();
/ / Convert to msec
/ / Add local time zone offset
/ / Get UTC time in msec
Utc = d.getTime () (d.getTimezoneOffset () * 60000);
/ / Create new Date object for different city
/ / Using supplied offset
Nd = new Date (utc (3600000 * offset));
/ / Return time as a string
Return "The local time in the" city "is" nd.toLocaleString ();
)
/ / Get Bombay time
Alert (calcTime ( 'Bombay', '5.5'));
/ / Get Singapore time
Alert (calcTime ( 'Singapore', '8'));
/ / Get London time
Alert (calcTime ( 'London', 'a'));
</ Script>
</ Head>
<body>
</ Body>
</ Html>
Here, function calcTime () to accept a city name and its international standard time (UTC) offset value (by the hour). Then the internal implementation of all the terms described above, and return to the city included a string Time.
Following is the list of the contents of A output some samples.
Mumbai local time for the August 1, 2005, Monday 16:43:51
Singapore local time for the August 1, 2005, Monday 19:13:51
London local time for the August 1, 2005, Monday, 0:13:51
Next time you sit down to prepare for your time zone page script, this script is expected to save you some time. Enjoy! !
Tags: css javascript






Leave a Reply