javascript - 为什么一串数字的工作方式与新日期中的实际数字不同?

标签 javascript string date numbers

为什么一串数字的工作方式与 new Date() 中的实际数字不同:

var myfirstDate = new Date("2013, 10, 15"); //returns Tue Oct 15 2013 00:00:00 GMT-0500 (CDT)
var mysecondDate = new Date(2013, 9, 15); // also returns Tue Oct 15 2013 00:00:00 GMT-0500 (CDT)

myfirstDate.value == mysecondDate.value; //returns true

我看了几个教程,上面甚至没有提到像 myfirstDate 这样的字符串的想法。 JavaScript 会自动解析字符串吗?

最佳答案

参见the docs .

您实际上调用了两个不同的构造函数。

第一个被解析为人类可读的日期:

new Date(dateString)

第二个需要 3 个或更多参数,提供年份、基于 0 的月份数字和日期

new Date(year, month, day [, hour, minute, second, millisecond]);

year

Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.

月份

Integer value representing the month, beginning with 0 for January to 11 for December.

Integer value representing the day of the month (1-31).

关于javascript - 为什么一串数字的工作方式与新日期中的实际数字不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19414883/

相关文章:

javascript - 用于 AngularJS 的 jQuery "$.when().done()"

javascript - 网速慢时如何显示基本的html页面?

arrays - 字符串数组作为哈希函数键?

java - 按时间对一周中的几天进行排序

swift - 日期格式模板中区域设置的首选小时格式

javascript - jQuery UI DatePicker 仅显示月份年份

javascript - 通过 JavaScript 设置按钮文本

javascript - 如何让文本字段为空?这是怎么回事?

python - 从字符串中删除所有标点符号,除非它在数字之间

C - 像多维数组一样使用字符指针数组是否会修改存储的数据?