If our application is aimed at all over the world, when we want to show the time of the background (server) to the users in different regions, this time should be converted to the time of the user's client, and then presented to the user, that is, the following getTimezoneOffset ()
If you are simply converting local time to other time zones, use getTimezoneOffset ()
Concepts to understand: GMT
A place on the Greenwich Meridian, or Zero zone (in the time zone), is called Greenwich Mean Time, also known as the world. (More detailed concepts do not say, here we do not need.) For example, we China is East eight district, Beijing time is (gmt+08:00)
Get the time difference between local and GMT: New Date (). getTimezoneOffset (), in minutes.
Known GMT, conversion to local correct time
local time = GMT-Jetlag
Known local time, conversion corresponds to GMT:
GMT = local time + jet lag
Known local time, conversion time of other time zones
Because the time interval difference is in the hour unit. So calculate the time of 0 time zone, then subtract or add the corresponding hour (East N area will +n hours, West N area will N-hour). In order to facilitate the calculation, the East N area is a positive number, the West N area to do negative, namely:
Target timezone time = local time + slack + time zone interval
Example 1: Convert local current time to 0 time zone
//Calculate the time difference and convert to milliseconds: varoffset =NewDate (). getTimezoneOffset () * 60 * 1000; //figure out the current time: varNowdate =NewDate (). GetTime (); //figure out the corresponding green position time. varGmtdate =NewDate (nowdate + offset);//Wed Apr 22:27:02 gmt+0800 (CST) //convert cost to TIME format vargmtdateinlocalstring = Gmtdate.tolocalestring ();//2016/4/20 pm 10:27:02
Example 2: Time to convert the local current time to the East 2 zone
//East 2, East time zone, positive. varZoneoffset = 2; //Calculate the time difference and convert to milliseconds: varOffset2 =NewDate (). getTimezoneOffset () * 60 * 1000; //figure out the current time: varNowDate2 =NewDate (). GetTime (); //at this time, the East 2 zone varCurrentzonedate =NewDate (nowDate2 + offset2 + zoneoffset*60*60*1000); Console.log ("East 2 is now:" +currentzonedate);
JS Time difference Conversion gettimezoneoffset ()