different result for yyyy-mm-dd and yyyy/mm/dd in javascript when passed to “new Date”
我在nodejs repl下执行下面的语句,我在同一日期得到了两个不同的结果
1 2 3 4 | var dateStr1 ="2015/03/31"; var dateStr2 ="2015-03-31"; var date1 = new Date(dateStr1);//gives Tue Mar 31 2015 00:00:00 GMT+0530 (IST) var date2 = new Date(dateStr2);//gives Tue Mar 31 2015 05:30:00 GMT+0530 (IST) |
在第一个小时中,min,seconds都是0,而在第二个小时中,min默认设置为时区小时,min为5:30。
它归结为
The date time string may be in ISO 8601 format. For example,"2011-10-10" (just date) or"2011-10-10T14:48:00" (date and time) can be passed and parsed. The UTC time zone is used to interpret arguments in ISO 8601 format that do not contain time zone information (note that ECMAScript ed 6 draft specifies that date time strings without a time zone are to be treated as local, not UTC)
号
您的第一个日期格式
链接文档中的"假定时区差异"标题将更加详细:
Given a date string of"March 7, 2014", parse() assumes a local time zone, but given an ISO format such as"2014-03-07" it will assume a time zone of UTC. Therefore Date objects produced using those strings will represent different moments in time unless the system is set with a local time zone of UTC. This means that two date strings that appear equivalent may result in two different values depending on the format of the string that is being converted (this behavior is changed in ECMAScript ed 6 so that both will be treated as local).
号
根据ecmascript标准,第一个字符串
第二个字符串,