Arrangement of time zone knowledge in javascript UTC GMT

What is GMT, UTC?

GMT: Greenwich Mean Time
UTC: Coordinated Universal Time
GMT and UTC are not exactly the same, but they all refer to 0 time zone time, which can be regarded as synonymous in general



As a javascript developer, is not very familiar to GMT?
In fact, GMT is what we call new Date (); when the returned string "Wed Jan 06 2021 17:48:30 GMT + 0800 ( China Standard Time)" in GMT

GMT + 0800: China is the East Area 8, it is GMT + 08:00 (Eastern negative due west; +0800 That is +08: 00, meaning; interested go online to learn)



What is jet lag?

The "time difference" in this article refers to the time difference in javascript, not the geographical time difference

The time difference in javascript refers to the time difference between Greenwich Mean Time and local time,
that is,
Greenwich Mean Time-Local Time = Time Difference

After simple conversion, the following conclusions can be drawn:

  • UTC + 8 = GMT + 8 = Beijing Time (China is the East 8th District)
  • Greenwich Mean Time + 8 = Beijing Time
  • Greenwich Mean Time-Beijing Time = -8
  • Greenwich Mean Time-Local Time = Time Difference



getTimezoneOffset()

javascript provides a method to get the time difference

The getTimezoneOffset() method returns the time difference between Greenwich Mean Time and local time in minutes

usage:

new Date().getTimezoneOffset() // -480



javascript timestamp

The number of milliseconds between the specified date and time and midnight (GMT time) on January 1, 1970

Two points are emphasized here, two points are indispensable

  • January 1, 1970 0:00:00:00
  • Greenwich Mean Time



Set date and time

new Date() can accept several values ​​without passing a value

new Date (timestamp)
first calculates GMT based on the number of milliseconds passed in, and then converts GMT to the local time zone.
So passing in 0 is not 0 point but 8 points (China is the East 8th district)

new Date (time string)
time string can be like this "1970-1-10 0:0:0"
can also be added to specify the time zone "1970-1-10 0:0:0 gmt+0900"
do not specify Time zone, the default is GMT+0800 (China is the East 8th zone) and
finally converted to the local time zone

new Date(year, month, day, hours, minutes, seconds, milliseconds) At
least two data should be passed in, pass null or not, it is the value of the current time,
pass in the local time zone,
get the local time zone



Get timestamp

getTime()
var d = new Date();
var n = d.getTime(); The
obtained timestamp n is converted from the local time to GMT, and then calculated as 0 :00:00:00 on January 1, 1970 Millisecond difference

Date.UTC()
Date.UTC (year, month, day, hour, minute, second, millisecond) to
get the timestamp, which is directly GMT, and then calculate the millisecond difference with January 1, 1970 0: 00:00:00 value

The UTC() method returns the number of milliseconds from January 1, 1970 to the specified date according to universal time

The parameter cannot be empty, it can be null, missing value or null is supplemented with 0



UTC, time difference, local time relationship code expression

var _date = new Date();

var _UTC = Date.UTC(_date.getFullYear(), _date.getMonth(), _date.getDate(), _date.getHours(), _date.getMinutes(), _date.getSeconds(), _date.getMilliseconds());
var _BJ = _date * 1;
var _timeDifference = new Date().getTimezoneOffset() * 60 * 1000

_BJ - _UTC === _timeDifference // true

//end

Guess you like

Origin blog.csdn.net/u013970232/article/details/112286499