如何获取月份javascript列表

时间:2013-06-04 03:56:02

标签: javascript jquery html

我正试图在javascript中获取月份列表!如何只使用javascript来完成这项工作!

谢谢!

3 个答案:

答案 0 :(得分:29)

既然你应该使用moment来处理日期,你也可以在这里使用它! ;)

moment.months()已添加到2.3.0

const moment = require('moment');
moment.months() // returns a list of months in the current locale

答案 1 :(得分:26)

据我所知,你只能通过硬编码来获得阵列。

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

或者您可以使用一些具有此列表硬编码的javascript库。

答案 2 :(得分:-5)

请使用这个:

如果您想获得当月:

var theMonths = ["January", "February", "March", "April", "May",
  "June", "July", "August", "September", "October", "November", "December"];
var today = new Date();
var aMonth = today.getMonth();
var i;
for (i=0; i<12; i++) {
  document.write(theMonths[aMonth]);
  aMonth++;
  if (aMonth > 11) {
  aMonth = 0;
}

如果你想获得所有月份:

var theMonths = ["January", "February", "March", "April", "May",
  "June", "July", "August", "September", "October", "November", "December"];
for (i=0; i<12; i++) {
  document.write(theMonths[i]);
}