Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 月份值总是打印错误_Javascript - Fatal编程技术网

Javascript 月份值总是打印错误

Javascript 月份值总是打印错误,javascript,Javascript,我得到一个字符串,如“2016年3月”。我想在number中查找月份。我得到了奇怪的/错误的结果 var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]; new Date(str.split(", ")[1], months.indexOf(str.split(", ")[0]) + 1, new Date().getD

我得到一个字符串,如
“2016年3月”
。我想在
number
中查找
月份
。我得到了奇怪的/错误的结果

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul",
                 "Aug", "Sept", "Oct", "Nov", "Dec"];

new Date(str.split(", ")[1], months.indexOf(str.split(", ")[0]) + 1, new Date().getDate());
为什么上面的代码打印我<代码>四月而不是三月

Fri Apr 01 2016 00:00:00

months.indexOf(str.split(“,”[0])+1
不正确。月份是0索引的。将其保留为月份。indexOf(str.split(“,”[0])
月份。indexOf(str.split(“,”[0])+1
不正确。月份是0索引的。将其保留为
months.indexOf(str.split(“,”[0])

,因为您要向其添加1

new Date(str.split(", ")[1], months.indexOf(str.split(", ")[0]) + 1, new Date().getDate());
移除它

newdate(str.split(“,”[1]),months.indexOf(str.split(“,”[0]),newdate().getDate())


您将很好,因为您正在向其中添加1

new Date(str.split(", ")[1], months.indexOf(str.split(", ")[0]) + 1, new Date().getDate());
移除它

newdate(str.split(“,”[1]),months.indexOf(str.split(“,”[0]),newdate().getDate())


你会没事的,JavaScript月份是零基索引,所以三月是第二个月,四月是第三个月,因为你的数组索引是以
[0]
而不是
[1]
JavaScript月份是零基索引,所以三月是第二个月,四月是第三个月,因为你的数组索引是以
[0]
而不是
[1]