The toISOString() method returns a string in the simplified extended ISO format (ISO 8601) that is always 24 or 27 characters long (YYYY-MM-DDTHh: MM: sS.ssSz or ± YYYYYY-MM-DDTHh: MM: SS.sssz). The time zone is always zero UTC offset, as indicated by the suffix “Z”.

Therefore, it is necessary to add 8 time zones to change to Beijing time. The scheme is as follows

The solution

Const date = new date (+new date () + 8 * 3600 * 1000); const currentMonth = date.toISOString().substr(0, 4) + date.toISOString().substr(5, 2); Var year=new Date().getFullYear(); // iF (year< 1900) year = year + 1900; var month = new Date().getMonth() + 1; // if (month < 10) month = '0' + month; var day = new Date().getDate(); // if (day < 10) day = '0' + day; var hour = new Date().getHours(); // if (hour < 10) hour = '0' + hour; var minute = new Date().getMinutes(); // minute if (minute < 10) minute = '0' + minute; var second = new Date().getSeconds(); // second if (second < 10) second = '0' + second; var str=year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; console.log(str) // "2021-07-29 11:35:23"Copy the code

What is the GMT

GMT (Greenwich Mean Time).

It sets the time for the sun to pass through the Royal Greenwich Observatory on the outskirts of London, England, at 12 noon each day.

New Date() // Wed Jul 28 2021 17:34:11 GMT+0800 {}Copy the code

What is the UTC

Coodinated Universal Time (UTC) is also known as Universal Time. Because of the different abbreviations for CUT and TUC, the compromise was called UTC.

GMT vs UTC

GMT is pre-Universal standard time, UTC is current Universal Standard time.

UTC is more accurate than GMT, keeping time in atomic time and adapting to modern times.

But in cases where accuracy is not required to the second, the two can be considered equivalent.

Each year the Greenwich Observatory issues time information, based on UTC.

The time zone

From the Greenwich prime meridian, each 15° east or west of longitude is divided into a time zone in which the same standard time is used.

But in practice, for administrative convenience, one country or one province is often grouped together. So time zones are not divided strictly by north-south lines, but by natural conditions. Also: Because there is currently no international body that approves countries to change time zones. Some countries change their time zones for specific reasons.

The world is divided into 24 standard time zones, with adjacent time zones one hour apart.