這個問題在這里已經有了答案: 在哪里可以找到 javascript 本機函式源代碼?[重復] (3 個回答) 什么是 JavaScript 中的有效日期時間字串? (2 個回答) 損壞的 v8 日期決議器有什么解決方法嗎? (3 個回答) 為什么 Date.parse 給出不正確的結果? (11 個回答) 2 天前關閉。
我試圖看看依靠執行 aconst myNewDate = new Date(dateString)然后使用 an 的_.isDate(myNewDate)結果是否足以驗證它是一個“有效日期”,但后來我想:我能收到dateString什么?
我想到要執行以下操作:
for(i=0;i<110;i ) { console.log(i, new Date(String(i)))}
這是我得到的結果:
0 2000-01-01T06:00:00.000Z
1 2001-01-01T06:00:00.000Z
2 2001-02-01T06:00:00.000Z
3 2001-03-01T06:00:00.000Z
4 2001-04-01T06:00:00.000Z
5 2001-05-01T05:00:00.000Z
6 2001-06-01T05:00:00.000Z
7 2001-07-01T05:00:00.000Z
8 2001-08-01T05:00:00.000Z
9 2001-09-01T05:00:00.000Z
10 2001-10-01T05:00:00.000Z
11 2001-11-01T06:00:00.000Z
12 2001-12-01T06:00:00.000Z
13 Invalid Date
14 Invalid Date
15 Invalid Date
16 Invalid Date
17 Invalid Date
18 Invalid Date
19 Invalid Date
20 Invalid Date
21 Invalid Date
22 Invalid Date
23 Invalid Date
24 Invalid Date
25 Invalid Date
26 Invalid Date
27 Invalid Date
28 Invalid Date
29 Invalid Date
30 Invalid Date
31 Invalid Date
32 2032-01-01T06:00:00.000Z
33 2033-01-01T06:00:00.000Z
34 2034-01-01T06:00:00.000Z
35 2035-01-01T06:00:00.000Z
36 2036-01-01T06:00:00.000Z
37 2037-01-01T06:00:00.000Z
38 2038-01-01T06:00:00.000Z
39 2039-01-01T06:00:00.000Z
40 2040-01-01T06:00:00.000Z
41 2041-01-01T06:00:00.000Z
42 2042-01-01T06:00:00.000Z
43 2043-01-01T06:00:00.000Z
44 2044-01-01T06:00:00.000Z
45 2045-01-01T06:00:00.000Z
46 2046-01-01T06:00:00.000Z
47 2047-01-01T06:00:00.000Z
48 2048-01-01T06:00:00.000Z
49 2049-01-01T06:00:00.000Z
50 1950-01-01T06:00:00.000Z
51 1951-01-01T06:00:00.000Z
52 1952-01-01T06:00:00.000Z
53 1953-01-01T06:00:00.000Z
54 1954-01-01T06:00:00.000Z
55 1955-01-01T06:00:00.000Z
56 1956-01-01T06:00:00.000Z
57 1957-01-01T06:00:00.000Z
58 1958-01-01T06:00:00.000Z
59 1959-01-01T06:00:00.000Z
60 1960-01-01T06:00:00.000Z
61 1961-01-01T06:00:00.000Z
62 1962-01-01T06:00:00.000Z
63 1963-01-01T06:00:00.000Z
64 1964-01-01T06:00:00.000Z
65 1965-01-01T06:00:00.000Z
66 1966-01-01T06:00:00.000Z
67 1967-01-01T06:00:00.000Z
68 1968-01-01T06:00:00.000Z
69 1969-01-01T06:00:00.000Z
70 1970-01-01T06:00:00.000Z
71 1971-01-01T06:00:00.000Z
72 1972-01-01T06:00:00.000Z
73 1973-01-01T06:00:00.000Z
74 1974-01-01T06:00:00.000Z
75 1975-01-01T06:00:00.000Z
76 1976-01-01T06:00:00.000Z
77 1977-01-01T06:00:00.000Z
78 1978-01-01T06:00:00.000Z
79 1979-01-01T06:00:00.000Z
80 1980-01-01T06:00:00.000Z
81 1981-01-01T06:00:00.000Z
82 1982-01-01T06:00:00.000Z
83 1983-01-01T06:00:00.000Z
84 1984-01-01T06:00:00.000Z
85 1985-01-01T06:00:00.000Z
86 1986-01-01T06:00:00.000Z
87 1987-01-01T06:00:00.000Z
88 1988-01-01T06:00:00.000Z
89 1989-01-01T06:00:00.000Z
90 1990-01-01T06:00:00.000Z
91 1991-01-01T06:00:00.000Z
92 1992-01-01T06:00:00.000Z
93 1993-01-01T06:00:00.000Z
94 1994-01-01T06:00:00.000Z
95 1995-01-01T06:00:00.000Z
96 1996-01-01T06:00:00.000Z
97 1997-01-01T06:00:00.000Z
98 1998-01-01T06:00:00.000Z
99 1999-01-01T06:00:00.000Z
100 0100-01-01T05:50:36.000Z
101 0101-01-01T05:50:36.000Z
102 0102-01-01T05:50:36.000Z
103 0103-01-01T05:50:36.000Z
104 0104-01-01T05:50:36.000Z
105 0105-01-01T05:50:36.000Z
106 0106-01-01T05:50:36.000Z
107 0107-01-01T05:50:36.000Z
108 0108-01-01T05:50:36.000Z
109 0109-01-01T05:50:36.000Z
所以,稍微分析一下:
- 從 0 到 4,該值用于 6:00 的“月”
- 從 5 到 10,該值再次用于“月”,但在 5:00(為什么?)
- 11和12,數值是“月”,但是回到6:00
- 從 13 到 31 是無效日期(為什么?)
- 從 32 到 49,現在該值屬于年份:2000 值(為什么?)
- 從 50 到 99,現在該值屬于年份:1900 值(為什么?)
- from 100 to 110 (and even more, I tried 9999 with the same result) it belongs to the year, literally: 100 becomes year 0100. But the time, is set to 5 hours, and minutes and seconds belong to my current computer minutes and seconds (why?)
This is extremely confusing. I'd love to find the source of this.
Techical notes:
- This is clearly javascript code.
- I've used node 12.16.3.
- Same result if I run it in Chrome's console.
- In Firefox, it returns all as invalid dates.
uj5u.com熱心網友回復:
從 0 到 4,該值用于 6:00 的“月”
默認偏移量將是您的語言環境,在這些月份中為 -6。根據作者 Ryan Dahl 的說法,“ KJS兼容性的默認年份為 0(=> 2000)”。您可能已經注意到年份從 2000 年更改為 2001 年……我認為這是一個錯誤。請參閱:現有答案
從 5 到 10,該值再次用于“月”,但在 5:00(為什么?)
11和12,數值是“月”,但是回到6:00
語言環境中的 DST 更改,這是默認偏移量
從 13 到 31 是無效日期(為什么?)
因為提供的字串不是有效的月份或 YYYY,所以它被決議為 DD,并且只有一個日期值的 Date 沒有意義。為什么它不默認MM或YY不清楚……推測,原因可能是決議時YY和DD之間的歧義。
從 32 到 49,現在該值屬于年份:2000 值(為什么?)
從 50 到 99,現在該值屬于年份:1900 值(為什么?)
PHP strptime、Pythondatetime.strptime和 UNIX Cstrptime 假設 00-68 年屬于 2000 年,69-99 年屬于 1900 年。這是為了方便 API,假設較低的數字 32:49,更接近本世紀,屬于本世紀并且更高的數字 50:99,更接近上一個世紀,指的是所說的世紀。
從 100 到 110(甚至更多,我用相同的結果嘗試了 9999)它屬于年份,字面意思是:100 變成了 0100 年
YYY(當 > 99 時)實際上是YYYAD,類似于 ISO 年
但是時間,設定為5小時,分秒屬于我當前電腦的分秒(為什么?)
時區確實會隨著時間的推移而變化,并且語言環境決議將嘗試考慮這些。
> (new Date("1883-05-31")).getTimezoneOffset()
350
> (new Date("1893-05-31")).getTimezoneOffset()
360
> (new Date("2023-05-31")).getTimezoneOffset()
300
但是,Unix 紀元之前的日期從不包括時區名稱,并且 1984 年 11 月 19 日之前的日期不使用現代 GMT 偏移量,因為它尚未建立。
> format(new Date("1883", "10", "19"))
'Monday, November 19, 1883 at 12:00:00 AM GMT-06:00'
> format(new Date("1883", "10", "18"))
'Sunday, November 18, 1883 at 12:00:00 AM GMT-05:50:36'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/350572.html
