The author:
gauseen
The original:
https://github.com/gauseen/blog
new Date()
Get the current date, as I’m sure you know, as follows:
New Date() Thu Apr 16 2020 13:55:25 GMT+0800 Thu Apr 16 2020 13:55:25 GMT+0800 Thu Apr 16 2020 13:55:25 GMT+0800 Thu Apr 16 2020 13:55:25 GMT+0800 Thu Apr 16 2020 13:55:25 GMT+0800 Thu Apr 16 2020 13:55:25 GMT+0800 Thu Apr 16 2020 13:55:25 GMT+0800
Have you noticed what the GMT+0800 mark means? How do you compute it?
GMT and UTC concepts
Mean Greenwich Time (GMT)
Greenwich Mean Time (GMT) is the local Mean solar Time at the Royal Greenwich Observatory, located on the outskirts of London, United Kingdom
Noon Greenwich Mean Time refers to the time when the mean sun crosses the Greenwich meridian (i.e. the highest point above Greenwich). Since the Earth’s daily rotation is somewhat irregular and slowly slowing down, Greenwich Mean Time, based on astronomical observations themselves, has been replaced by Coordinated Universal Time (UTC), which is measured by atomic clocks
Since February 5, 1924, the Greenwich Observatory has been responsible for sending hourly timings around the world
Coordinated Universal Time (UTC)
Coordinated Universal Time (Coordinated Universal Time, UTC) is also described as: Also known as World Unified Time, World Standard Time, it is the most important world time standard, based on the length of atomic time second, in time as close as possible to Greenwich Mean Time.
UTC and GMT
For most uses, UTC is interchangeable with GMT, which stands for the local time in a time zone (London, UK), and UTC, which stands for the time standard.
The time zone
Time zone 0 is used in London, UK, and time zone 0 is used in all parts of the world. Local time zone time is calculated by adding or subtracting the corresponding time difference.
The earth rotates “from west to east”, and the time on the east side is several hours earlier than the time on the west side.
London time is divided into daylight saving time and winter time. The time difference between the UK and China is 7 hours. The difference between England and China in winter is 8 hours.
Beijing Time (Chinese Standard Time)
GMT+0800 stands for China Standard Time, which is the UK 0 time zone (UTC/GMT) plus 8 hours
Js new Date() compatibility issue
Here’s an interesting example from the Chrome browser:
New Date('2020-4-20') Mon Apr 20 2020 00:00:00 GMT+0800 (China Standard Time) new Date('2020-04-20') Mon Apr 20 2020 08:00:00 0800 GMT + 8 hours (China standard time) / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- new Date () '2020/4/20' Mon Apr 20, 2020 00:00:00 GMT+0800 (China Standard Time) New Date('2020/04/20') MON APR 20 2020 00:00:00 GMT+0800 (China Standard Time)
The MDN mentions that due to browser differences and inconsistencies, the use of the Date constructor to parse the Date string (or its equivalent, date.parse) is strongly not recommended. In support of the ISO 8601 format, date-only strings (such as “1970-01-01”) are treated as UTC instead of local time.
In other words:
- There is a leading zero hour
new Date('2020-04-20')
Is considered to beUTC (Zero Time Zone)Time, converted to Chinese standard time forMon Apr 20 2020 08:00:00 GMT+0800
It’s a lot different than what was passed in8Hours. - No leading zero hours
new Date('2020-4-20')
Is considered to beLocal time (Beijing, China: East 8th District)Is already the local time, so the Chinese standard time isMon Apr 20 2020 00:00:00 GMT+0800
, not much time to be the same as the passed value.
Firefox will parse all of the following to UTC time:
New Date('2020-4-20') Date 2020-04-20T00:00:00.000Z New Date('2020-4-20') Date 2020-04-20T00:00:00.000Z -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- new Date (' 2020/4/20) the Date the T16 2020-04-19:00:00. The new 000 z / / little 8 hours Date('2020/04/20') Date 2020-04-19T16:00:00.000z //
Note: Local time can be obtained through date.gethours () and other APIs
2020-04-19T16:00:00.000Z ISO 8601 specification, time format: YYYY-MM-DDTHH: MM: SS.SSSZ,
- T: An identifier that separates the date from the specific time and identifies the start of the time element
- Z: represents the time zone offset, and Z represents the UTC (zero time zone) time
Solving compatibility issues
The ‘2020-4-20’ format is parsed differently in each browser, so you can convert it to the ‘2020/4/20’ format to resolve parsing inconsistencies, as follows:
function getDate (dateStr) {
dateStr = dateStr.replace(/-/g, '/')
return new Date(dateStr)
}
Welcome to pay attention to the public number of ad-free articles: learn the front end
reference
- Greenwich Mean Time
- What is the relationship between UTC and GMT?
- UTC and time zone
- What is British Summer Time and Winter Time?
- An exploration of “time”