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);