Use JavaScript to get the time of different time zones

Source: Internet
Author: User
Tags time zones

Using javascript can easily display the local time on the page, but can I display the time in different time zones on the page? The answer is yes! We only need to do some simple date calculation to get the time of different time zones. The following is a detailed explanation.

Use the date object to get the local time

D = new date (); localtime = D. gettime (); // call the gettime () method of the data () object to display the number of milliseconds between January 1, 1970 and now.

Next, use the gettimezoneoffset () method of the data () object to find the local time offset value. By default, this method displays the result of Time Zone offset values in minutes, so you need to convert this value to milliseconds in an earlier calculation.

Localoffset = D. gettimezoneoffset () * 60000;

Then, the offset of the current time and the time zone is added to obtain the International Standard Time (expressed in milliseconds, because the calculation is still required, so no conversion is performed here ), then, add the offset of the time zone you want to know, get the time of that time, and then convert it to a time string using the date object.

UTC = localtime + localoffset; // obtain the International Standard Time Offset = 5.5; calctime = UTC + (3600000 * offset); ND = new date (calctime); document. write ('specifies the time zone: '+ nd. tolocalstring ());
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.