Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

javascript - 为什么Date.parse给出不正确的结果?(Why does Date.parse give incorrect results?)

Case One:(情况一:)

new Date(Date.parse("Jul 8, 2005"));

Output:(输出:)

Fri Jul 08 2005 00:00:00 GMT-0700 (PST)(2005年7月8日星期五00:00:00 GMT-0700(PST))

Case Two:(案例二:) new Date(Date.parse("2005-07-08")); Output:(输出:) Thu Jul 07 2005 17:00:00 GMT-0700 (PST)(Thu Jul 07 2005 17:00:00 GMT-0700(PST)) Why is the second parse incorrect?(为什么第二次解析不正确?)   ask by user121196 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Until the 5th edition spec came out, the Date.parse method was completely implementation dependent ( new Date(string) is equivalent to Date.parse(string) except the latter returns a number rather than a Date ).(在第5版规范发布之前, Date.parse方法完全依赖实现new Date(string)等效于Date.parse(string)但后者返回一个数字而不是Date )。)

In the 5th edition spec the requirement was added to support a simplified (and slightly incorrect) ISO-8601 (also see What are valid Date Time Strings in JavaScript? ).(在第5版规范中,添加了该要求以支持简化的(并且略有错误) ISO-8601 (另请参见JavaScript中有效的日期时间字符串是什么? )。) But other than that, there was no requirement for what Date.parse / new Date(string) should accept other than that they had to accept whatever Date#toString output (without saying what that was).(但是除此之外,除了必须接受任何Date#toString输出(不说那是什么)之外, 没有要求Date.parse / new Date(string)应该接受什么。) As of ECMAScript 2017 (edition 8), implementations were required to parse their output for Date#toString and Date#toUTCString , but the format of those strings was not specified.(从ECMAScript 2017(版本8)开始,要求实现解析Date#toStringDate#toUTCString的输出 ,但未指定这些字符串的格式。) As of ECMAScript 2019 (edition 9) the format for Date#toString and Date#toUTCString , have been specified as (respectively):(从ECMAScript 2019(版本9)开始, Date#toStringDate#toUTCString的格式分别指定为:) ddd MMM DD YYYY HH:mm:ss ZZ [(timezone name)](ddd MMM DD YYYY HH:mm:ss ZZ [(时区名称)])
eg Tue Jul 10 2018 18:39:58 GMT+0530 (IST)(例如,2018年7月10日星期二18:39:58 GMT + 0530(IST)) ddd, DD MMM YYYY HH:mm:ss Z(ddd,DD MMM YYYY HH:mm:ss Z)
eg Tue 10 Jul 2018 13:09:58 GMT(例如,2018年7月10日星期二13:09:58 GMT) providing 2 more formats that Date.parse should parse reliably in new implementations (noting that support is not ubiquitous and non–compliant implementations will remain in use for some time).(提供了另外2种格式, Date.parse应该在新的实现中可靠地对其进行解析(请注意,该支持并不是普遍存在的,并且不兼容的实现将在一段时间内继续使用)。) I would recommend that date strings are parsed manually and the Date constructor used with year, month and day arguments to avoid ambiguity:(我建议手动解析日期字符串,并将Date构造函数与年,月和日参数一起使用,以避免产生歧义:) // parse a date in yyyy-mm-dd format function parseDate(input) { var parts = input.split('-'); // new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]]) return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...