Home > front end >  Questions about the new Date () different time zones
Questions about the new Date () different time zones

Time:10-03

For example, China and Japan, China earlier than Japan a clock)
First of all, new Date () is to obtain the unit time (time code is written on the front end, is not in the server, to ignore an unsafe)
Such as I wrote in the Beijing time js code new Date (). The getDate () to get to the Beijing time 12 o 'clock the Japan open over there I wrote this page have access to the Japanese time 12 PM rather than 13 points, because if it is calculated according to the machine time to calculate it wouldn't!

CodePudding user response:

Different time zone, time affirmation is different, you can use timestamp, timestamp is no concept of time zones, so no matter in which time zone, time is the same, or use utc time as well

CodePudding user response:

Js convert UTC time into the local time zone (UTC GMT)

When we were in the web development is likely to involve foreign users or users in foreign countries, then will be the jet lag problems, for example, our time in China is 08:00, but now South Korea time is GMT, if need to show on the web page can be a problem, that as a front end how to solve this problem?

Front end by requesting access time is generally the timestamp format, the general is UTC timestamps (* UTC: one of the most close to the standard time time standards), and we need to display on the web page is GMT time, below is according to the local time for GMT time function of time and at any time zone:



1. The local time into arbitrary time zones (such as: the local time for South Korea's time) :

First we need to know that South Korea's time zone, this can be checked online, South Korea's time zone to the east district 9.

Copy the code
Copy the code
Var d=new Date ();
Var localTime=d.g etTime ();
Var localOffset=d.g etTimezoneOffset () * 60000;//getTimezoneOffset is () returns a minute, need to translate into ms
Var utc=localTime + localOffset;
Var offset=9;//time in South Korea, for example, the east area of 9
Var Korean=utc + offset (3600000 *);
Var nd=new Date (Korean);
The console. The log (" Korean is "+ nd. ToLocaleString ());
Copy the code
Copy the code


2. The UTC time into a local time zone (UTC time format is commonly "2017-11-16 T05: behold. 000 z");

Copy the code
Copy the code
ConvertUTCTimeToLocalTime: function (UTCDateString) {
if(! UTCDateString) {
Return the '-';
}
Function formatFunc (STR) {//formatting shows
Return the STR & gt; 9? STR: '0' + STR
}
Var date2=new Date (UTCDateString);//this is the key
Var year=date2. GetFullYear ();
Var mon=formatFunc (date2, getMonth () + 1);
Var day=formatFunc (date2 getDate ());
Var hour=date2. GetHours ();
Var=noon hour & gt;=12? 'PM' : 'AM';
Hour=hour>=12? Hour - 12: hour;
Hour=formatFunc (hour);
Var min=formatFunc (date2 getMinutes ());
Var dateStr=year + '-' + mon + '-' + '+ noon day + + + hour +', '+' 'min;
Return dateStr;
},
The console. The log (convertUTCTimeToLocalTime (" the 2017-11-16 T05: behold. 000 z "));
//the 2017-11-16 PM 01:23
  • Related