JS TimeZone Time Conversion

Source: Internet
Author: User
Business Scenario:

Page Server time is the East eight area time, page JS function needs to compare server time and user local time, in order to be compatible with the world time, need to convert the user local time to the East eight area time. Basic Concepts GMT

The place on the Greenwich Meridian, or the region of the Zero region (the time zone), is called Greenwich Mean Time, also called the world. (More detailed concepts do not say, here we do not need.) For example, China is East Eight, Beijing time is (gmt+08:00)

Get the local and Greenwich time difference:new Date (). getTimezoneOffset (), in minutes. known GMT, convert local correct time

local time = GMT- time difference is known to local times, conversion corresponds to GMT:

GMT = local time + time difference known local time

Because the difference in time intervals is in hours. So calculate the time of 0 time zone, then subtract or add corresponding hour can be (East N area will +n hour, West N District will be N-hour). In order to facilitate the calculation, the East N region to do positive numbers, West N region to do negative, that is: target time zone time = local time + jet lag + time zone interval example: Convert local time to East eight area time

var timezone = 8; Target time zone time, East eight
var offset_gmt = new Date (). getTimezoneOffset ()/local time and Greenwich difference, in minutes
var nowdate = new Date (). GetTime (); The number of milliseconds between the local time from Midnight January 1, 1970 (GMT time)
var targetdate = new Date (nowdate + offset_gmt * 1000 + timezone * 60 * * 1000);
Console.log ("East 2 area is now:" + targetdate);

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.