preface

In the work, we often use js time object Date, such as a direct new instance of a Date object let Date = new Date(), to obtain the current time. Let’s take a look at the output to lead to today’s questions:

  1. In the browser console output:

    let date = new Date(a)// Print the current time directly
    console.log(date); // Thu Dec 09 2021 16:55:48 GMT+0800
    
    // Get the timestamp
    console.log(date.getTime()); / / 1639040148611
    
    // Get the local time
    console.log(date.getHours()); / / 16
    
    // Get the UTC time
    console.log(date.getUTCHours()); / / 8
    
    // Gets the time difference between the UTC standard time and the local time zone, in minutes
    console.log(date.getTimezoneOffset()); // -480 480/60 = 8h, equivalent to the UTC standard time difference of 8h
    Copy the code
  2. Output from node:

    let date = new Date(a)// Print the current time directly
    console.log(date); / / the 2021-12-09 T08:58:34. 625 z
    
    // Get the timestamp
    console.log(date.getTime()); / / 1639040314625
    
    // Get the local time
    console.log(date.getHours()); / / 16
    
    // Get the UTC time
    console.log(date.getUTCHours()); / / 8
    
    // Gets the time difference between the UTC standard time and the local time zone, in minutes
    console.log(date.getTimezoneOffset()); // -480 480/60 = 8h, equivalent to the UTC standard time difference of 8h
    Copy the code

Questions to ponder

  1. The current time output by the browser is directly converted to China standard timeThu Dec 09 2021 16:55:48 GMT+0800Here,GMT+0800What does t stand for?
  2. Each time has a timestamp. What is the output timestamp based on? Why are the timestamps different at different times?
  3. Each gets the time ofAPIThere should be a get for all of themUTCtheAPI, such asgetHours(),getUTCHours()theUTCWhat is it? Why is it standard time?
  4. When we usegetTimezoneOffset()To obtainUTCWhy does the time difference between standard time and local time in China return- 480.Minutes, that is8Hour, in other words, why China standard time andUTCStandard time has8Hour difference?
  5. nodeThe format of the current time of the end output isThe 2021-12-09 T08:58:34. 625 zIf you look carefully, you will find that if you add 8 hours to this time, it is exactly my current local timenodeThis time output by the end is known as standard timeUTC?

The above question has a few key words: GMT, timestamp, UTC, China Standard Time, time difference.

This article will learn a little bit about how these things come from, after learning, the above questions will naturally come to the answer in the heart, a little bit of patience to see the end.

GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time)

Greenwich is located on the outskirts of London and is located on the prime meridian (zero degree longitude). Greenwich Mean Time (GMT) refers to the Time at noon when the sun crosses the Greenwich Meridian. GMT was widely used as a basic time measurement system until 1960, mainly for the railway system after 1840.

But later, it was discovered that the Earth’s speed in its elliptical orbit was uneven, and that the moment might differ from actual solar time by as much as 16 minutes. The earth’s daily rotation is somewhat irregular and is slowly slowing down. As a result, Greenwich time is not very accurate, with errors every once in a while, so it is no longer used as standard time.

Then came Universal Time Coordinated (UTC), which is provided by atomic clocks. Since February 5, 1924, Greenwich Observatory has sent out hourly messages around the world. UTC is based on the exact time provided by standard GMT.

Unlike previous timekeeping systems, atomic time is very accurate and is not based on an average local solar time. However, when the Earth rotates unevenly, the time difference between atomic time and universal time accumulates over time, so UTC compensates for this by adding positive or negative leap seconds over time. Decisions about when to add leap seconds are made by the International Earth Rotation and Reference Systems Service in Paris.

Now, for convenience, we generally equate GMT with UTC, which are standard time.

Although UTC is more scientific and accurate, GMT is still more popular among watch players and collectors. Many see UTC as a way for Paris to claim its place as the world’s timekeeping center. In fact, it is a time measurement system based on atomic time and as close to universal time as possible in time. It emerged as a modern need for accurate timekeeping.

Time zone division

Now that we know the difference between UTC and GMT, let’s look at time zones.

We all know that the earth is a big ball, and people in different places see the sun at different times every day. When we have a dream at night, on the other side of the earth, maybe people are basking in the sun.

Some geography

Here’s a look back at middle and high school geography:

  1. Axis: An imaginary number line across the center of the North and South Poles on which the Earth rotates. Standing at the North Pole looking down on the earth, the earth rotates counterclockwise.
  2. Equator: The circle is the equator through the plane perpendicular to the earth’s axis in the center of the earth.
  3. The northernmost latitude at which the sun shines directly.
  4. Tropic of Cancer: The southernmost latitude of the point of direct sunlight.
  5. Prime meridian: A line (0 degrees longitude) connecting the central points of the north and south poles, indicating north and south. The longitude line that passes through the original site of Greenwich Observatory in London is known internationally as0 °Meridian, also called prime meridian.
  6. Latitude: Starting at the equator, the range is0-90 °North of the equator is latitude N and south of the equator is latitude S. Latitude usually refers to the latitude of the land (how long it is).
  7. North Pole: 90° N, N90
  8. South Pole: 90° s, S90

The total length of the prime meridian is about 40005KM, and the latitude is about 111KM (40005/360=111.1333).

Time zone division

Time zones are divided east or west from the prime meridians (lines 0 degrees east and 0 degrees west), dividing the world into 24 time zones, each of which accounts for 15 longitudes. (15*12=180), a difference of one hour.

Let’s take the eastern time zone as an example (the Western time zone is the same) :

The time zone The scope of
Middle time Zone (Prime Meridian, London, Greenwich, GMT, UTC) (0 degrees E, 0 degrees W) deviation 7.5 degrees left and right
East district 1 (15 degrees E) deviation 7.5 degrees left and right
East 2 area (30 degrees E) 7.5 degrees left and right
East 3 area (45 degrees E) offset 7.5 degrees left and right
East 4 areas (60 degrees E) deviation 7.5 degrees left and right
East 5 (75 degrees E) deviation 7.5 degrees left and right
6 area, (90 degrees E) deviation 7.5 degrees left and right
7 area, (105 e) offset 7.5 degrees left and right
East eight area (120 e) offset 7.5 degrees left and right
East nine area (135 degrees E) 7.5 degrees left and right
East district 10 (150 degrees E) deviation 7.5 degrees left and right
11 area, (165 degrees E) 7.5 degrees left and right
East 12 (International Dateline, West 12) (180 e, 180 W) deviation 7.5 degrees left and right

London is in the middle time zone, starting east, east 1, East 2… All the way to east 12. Beijing is in the east 8th district.

So when London time is 7am, Beijing time is 7+8= 15pm, 3pm.

Thu Dec 09 2021 16:55:48 GMT+0800 GMT+0800 means 8 hours longer than GMT, so Thu Dec 09 2021 8:55:48 GMT+0000. The output at the node end is 2021-12-09T08:58:34.625z, and the z at the end actually represents the standard time.

So when we use getTimezoneOffset() to get the difference between standard time and local time, we return -480, which is exactly 8 hours.

International dateline: longitude 180 degrees east and 180 degrees west

Prime meridian: 0 degrees east and 0 degrees west

Time increases by 1 hour for each time zone crossed east from the middle (prime meridian), and decreases by 1 hour for each time zone crossed west. Such as:

Take London Time (middle time zone) as an example. GMT is the standard time. If it is 12:00 am in London, it is 12:00 GMT+0000

12+1=13, 1:00 PM, 13:00 GMT+0100. 12+8=20 (20:00 GMT+0800) The time of west Zone 1 is 12-1=11, 11 am, which means 11:00 GMT-0100.

So an interesting thing happens. The East 12th district is 12 hours ahead of GMT and the West 12th district is 12 hours behind GMT, so when crossing from the east 12th district to the west 12th District, or crossing from the west 12th District to the east 12th district, the date must change by one day.

  1. East 12→ West 12, the date reduced by one day;
  2. 12 west → 12 East, date plus one day;

For example:

If London time 2021-12-09 12:00:00 GMT+0000,

12+12=24 2021-12-10 00:00:00 GMT+1200

2021-12-09 00:00:00 GMT-1200

So east 12th and West 12th are exactly one day off.

The time stamp

We know that every time in JS has a timestamp. The timestamp is the number of milliseconds since 1970-01-01 00:00:00. In programming languages, it’s much lighter to use this way to represent time. If the time is before 1970-01-01, it will have a negative timestamp.

1000ms = 1s

60s = 1min

60min = 1h

24 h = 1 day

/ / 1970-01-01 00:00:00
let date1 = new Date(0)
console.log(date1); / / the 1970-01-01 T00:00:00. 000 z

// One second after 1970-01-01 00:00:00, 1970-01-01 00:00:01
let date2 = new Date(1000)
console.log(date2); / / the 1970-01-01 T00:00:01onsaturday (UK time). 000 z

1970-01-01 00:00:00, 1969-12-31 23:59:59
let date3 = new Date(-1000)
console.log(date3); / / the T23:1969-12-31 59:59. 000 z
Copy the code

Beijing time.

China actually spans five time zones, east 5, East 6, East 7, East 8, East 9, with huge differences in time between east and west.

By 8am in Beijing, it is 9am in Harbin, 7am in Chengdu, 6am in Urumqi and 5am in the far west of Xinjiang.

For convenience, the whole country uses a uniform time: Beijing time.

So sometimes it’s sunny at seven o ‘clock in the morning in this place, but it’s just daylight in that place (although it’s all seven o ‘clock bill (´∀ ‘) back where I grew).

Beijing time currently used in China is not the local mean solar time of Beijing (116.4° E), but the local mean solar time of 120° E, a difference of about 14.5 minutes. Beijing time is told by 9 cesium atomic clocks and 2 hydrogen atom clocks at the National Time Center of Chinese Academy of Sciences in Lintong, Shaanxi Province, which is located in the geometric center of China’s territory, through precise comparison and calculation, and real-time comparison with time departments around the world by satellite.

National unification of the time is conducive to domestic economic and trade exchanges, conducive to management. Public transportation systems, especially planes and high-speed trains, increase the ease of management and coordination and reduce the corresponding costs. On the other hand, China has abolished daylight saving time, but employers can choose their own work hours flexibly. Many schools have delayed afternoon classes during the hot summer months to take care of people’s health, which makes more sense than using daylight saving time.

What is Daylight saving Time?

Daylight saving Time (DST) is a man-made time set in order to save energy. It is also known as daylight saving time and Daylight saving Time.

It is usually early in the summer when daylight saving time is enabled and the time is artificially moved forward by one hour.

For example, on a winter night, if it’s light at eight in the morning and dark at seven in the afternoon, You go to school at dawn (8 a.m.) and go to bed at dusk (7 p.m.).

When it’s summer, it’s light at 7 a.m. and dark at 6 p.m. If you go to school at the same time (8 a.m.) and go to bed at 7 p.m., that’s an hour after it’s light and an hour after it’s dark. As a result, you wake up late and go to bed late.

Therefore, some areas will use daylight saving time, artificially adjust the time one hour faster, one hour earlier in the morning, one hour earlier in the evening after school, that is, get up early, can also go to bed early, make full use of the light resources, school in advance at night, also do not need to consume more electricity, disguised saving the cost of lighting.

Reference: Baidu Encyclopedia daylight saving time

Think about the answer to the question

So, through the above learning, we can naturally know the answers to several questions in the preface.

  1. Thu Dec 09 2021 16:55:48 GMT+0800 Thu Dec 09 2021 16:55:48 GMT+0800

    GMT+0800, apparently, means eight hours ahead of standard time, which is the time in the east 8th ward.

    GMT is Greenwich Mean Time. Greenwich is located on the prime meridian (0° E and 0° W). It became standard time in the 19th century based on the time at which the sun shines across the meridian. Then, from the prime meridian, the time interval is divided every 15°. The time zone on the prime meridian is the middle time zone, with a one-hour difference between the east and the east and between the west and the west. Beijing is in the east 8th district, so it is expressed in GMT+0800, meaning 8 hours ahead of standard time.

  2. Each time has a timestamp. What is the output timestamp based on? Why are the timestamps different at different times?

    The timestamp is the number of milliseconds that have elapsed since 1970-01-01 00:00:00, so each time will have a unique millisecond value.

  3. GetHours (), getUTCHours(). What is UTC? Why is it standard time?

    UTC is coordinated world time. GMT was slightly incorrect every few minutes due to the uneven speed of the Earth in an elliptical orbit. Later, quantum timing was introduced, and GMT was calibrated every few minutes. Both UTC and GMT can be considered standard time, so any API that has UTC in it is designed to capture standard time.

  4. When we use getTimezoneOffset() to get the time difference between UTC and Local Time in China, why is it returned -480 minutes, or 8 hours? In other words, why is the time difference between UTC and Chinese standard time 8 hours?

    Because Beijing is in the East 8 zone, there is no time zone one hour apart, east 8 zone and the middle time zone exactly eight hours apart.

  5. The current time output from the node is in the format of 2021-12-09T08:58:34.625z. If you look carefully, you can find that adding 8 hours to the time is exactly my current local time. Is the time output from the node the so called UTC?

    There is no mistake, so to speak.

reference

www.jb51.net/article/776…

Baike.baidu.com/item/%E4%B8…

Zhidao.baidu.com/question/28…

Baike.baidu.com/item/%E5%8C…

Zhidao.baidu.com/question/18…

Baijiahao.baidu.com/s?id=164727…

Wenku.baidu.com/view/9e027f…

Baike.baidu.com/item/%E5%A4…