JavaScript converts local time to other time zones

Source: Internet
Author: User
Tags time zones local time

There is no doubt that JavaScript scripts allow you to easily display local time on a Web page by looking directly at the user's clock. But what if you want to show the time in different regions —--for example, if your part is in another country and you want to see "home" time rather than local time?
To do this, a variety of time calculations are required to convert the local time to the destination time. This article explains how to do these calculations.
The first step:
The first step in the matter is to get local time. In JavaScript, this can undoubtedly be done easily by initializing a data () object.
Create Date object for current location
D = new Date ();
By invoking the GetTime () method of the data () object, you can display the number of milliseconds from January 1, 1970 to the time.
Convert to msec since Jan 1 1970
LocalTime = D.gettime ();
Step Two:
Next, use the getTimezoneOffset () method of the data () object to find the local time offset value. By default, this method displays the result of the time zone offset value in minutes, so the value is converted to milliseconds in the previous 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 precedes global Standard Time (UTC) And that the positive return value represents the local time after global Standard Time (UTC).
Note: In case you want to know how I got 60000 of this multiplier factor, remember that 1000 milliseconds equals one second, and one minute equals 60 seconds. Therefore, convert the minute to milliseconds, which is 60 times 1000 equals 60000.
Step Three
Add local time to the local timezone offset value 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 a number of milliseconds from January 1, 1970 to the present. Let it be said for the time being, because there are some calculations to be made.
Fourth Step
After obtaining the International Standard Time (UTC), obtain the International Standard Time (UTC) Hour offset value for the target city, converting it to milliseconds, plus 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 want to know how I got 3600000 of this multiplier factor, remember that 1000 milliseconds equals one second, and an hour equals 3,600 seconds. Therefore, the hour is converted to milliseconds, with 3600 times 1000 equals 3600000.
At this point, the variable Bombay contains local time in Mumbai, India. This local time is expressed as a number of milliseconds from January 1, 1970 to the present. Obviously, this is not very reasonable, so we have to do some calculations.
Fifth Step
By initializing a new data () object and invoking the Tolocalstring () method of this object, we convert the calculated time value from the previous step to a date/time string that you can understand.
Convert msec value to date string
nd = new Date (Bombay);
Document.writeln ("Bombay time is" + nd.tolocalestring () + "<br>");
So the conversion is done!
Summarize
After understanding the above steps, let's take a look at this script (List a), which creates a compact, custom function Calctime () to perform all calculations and return a time value.
List A
<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 ' + 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 ', ' +1 '));
</script>
<body>
</body>
Here, the function calctime () accepts a city name and its international Standard Time (UTC) offset value (in hours). It then internally executes all the calculations described above and returns a string containing the local time of the city.
Here are some samples of the output from List A.
Mumbai local time is August 1, 2005, Monday 4:43 P.M. 51 seconds
Singapore local time is August 1, 2005, Monday 7:13 P.M. 51 seconds
London local time is August 1, 2005, Monday 12:13 P.M. 51 seconds

Source: http://www.jb51.net/article/17517.htm

JavaScript converts local time to other time zones

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.