如何在javascript中获取月份名称而不是月份号码?

时间:2017-07-13 07:19:54

标签: javascript

Here is my Code:
var listDate = [];
var startDate ='2017-02-01';
var endDate = '2017-02-10';
var dateMove = new Date(startDate);
var strDate = startDate;

while (strDate < endDate){
  var strDate = dateMove.toISOString().slice(0,10);
  listDate.push(strDate);
  dateMove.setDate(dateMove.getDate()+1);
};
console.log(listDate);

//["2017-02-01", "2017-02-02", "2017-02-03", "2017-02-04", "2017-02-05", "2017-02-06", "2017-02-07", "2017-02-08", "2017-02-09", "2017-02-10"]

它为我提供了像2017-02-01这样的月份编号的输出,但是我需要月份名称而不是月份编号。请帮助。

1 个答案:

答案 0 :(得分:4)

尝试这种简单的方法

var monthNames = ["January", "February", "March", "April", "May","June","July", "August", "September", "October", "November","December"

];

var d = new Date();
console.log("The current month is " + monthNames[d.getMonth()]);