我想更改javaScript中的日期格式,我发现这个代码是" Mike Christensen"这非常好。 但我的问题是在 var month line 3 的javascript数组代码中使用两个括号的含义是什么。
var mydate = new Date(form.startDate.value);
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()];
var str = month + ' ' + mydate.getFullYear();
答案 0 :(得分:2)
他使用从mydate.getMonth()获取月份数的月份数字索引月份数组
在这段代码中,mydate.getMonth()是11,因为他索引月份数月[11]将是12月
var mydate = new Date();
console.log(mydate.getMonth());
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()];
console.log(month);
var str = month + ' ' + mydate.getFullYear();

希望你理解
答案 1 :(得分:0)
第一对括号是创建数组,请将其视为
var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
下一对括号是按索引检索数组的操作。
someArr[index]
在你的情况下
month[11]
就是这样,作者只是通过索引获取刚刚创建的数组的元素,其中index是从另一个变量中提取的。
答案 2 :(得分:0)
执行此操作:使用先前声明的数组中的元素来初始化var月份是一种非常便宜的方式...(可以节省你多写一行但是恕我直言会减少代码的可读性......) 所以打破了这个:
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"] [mydate.getMonth()];
与
相同var monthOptionsArray = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var monthIndex = mydate.getMonth();
var monthActual = monthOptionsArray[monthIndex];
var mydate = new Date();
console.log(mydate.getMonth());
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]; monthI = month[1];
console.log(monthI);
var str = month + ' ' + mydate.getFullYear();