Use JavaScript script to convert local time to other time zones

Source: Internet
Author: User
Tags date object contains time zones local time return string variable
Scripts | transformations

There is no doubt that JavaScript scripts allow you to easily display local time on a Web page by directly viewing the user's clock. But what if you want to show different areas of time —--for example, if your part is in another country, what do you do if you want to see "home" time instead of local time?

To do this, a variety of time calculations are required to convert local time to destination time. This article explains how to do these calculations.

First step:

The first step in the business is to get local time. In JavaScript, this can undoubtedly be done easily by initializing a Data () object.

Create Date object for the current location

D = new Date ();

By calling the GetTime () method of the Data () object, you can display the number of milliseconds between January 1, 1970 and that time.

Convert to msec since 1 1970

LocalTime = D.gettime ();

Step Two:

Next, the local time offset value is identified by the getTimezoneOffset () method of the Data () object. By default, this method displays the time zone offset value results in minutes, so the value is converted to milliseconds in earlier calculations.

Obtain local UTC offset and convert to msec

Localoffset = D.gettimezoneoffset () * 60000;

Note that the negative return value of thegetTimezoneOffset () method indicates that local time precedes global Standard Time (UTC), whereas a positive return value indicates that local time is after global Standard Time (UTC).

Note : In case you want to know how I got the multiplier of 60000, remember that 1000 milliseconds equals one second, and one minute equals 60 seconds. Therefore, the Minutes are converted to milliseconds, with 60 times 1000 equals 60000.

Third Step

The current international Standard Time (UTC) is added with the local time zone offset value.

Obtain UTC time in msec

UTC = LocalTime + localoffset;

Here, the variable UTC Contains the current international Standard Time (UTC). However, this time is represented by the number of milliseconds that are contained on January 1, 1970. Let it be said for the time being, because there are some calculations to be done.

Fourth Step

After the International Standard Time (UTC) is obtained, the International Standard Time (UTC) Hour offset value for the target city is obtained, which is converted to milliseconds, plus 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 the multiplier of 3600000, remember that 1000 milliseconds equals one second, and one hour equals 3,600 seconds. So, converting hours to milliseconds, 3600 times 1000 equals 3600000.

At this point, the variable Bombay contains local time in Mumbai, India. This local time is represented by the number of milliseconds that are contained in the January 1, 1970 to date. 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 the object, we convert the time value computed in the previous step into a date/time string that you can read.

Convert msec value to date string

nd = new Date (Bombay);

Document.writeln ("Bombay time is" + nd.tolocalestring () + "<br>");

So the conversion is done!

[1] [2] Next page



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.