jQuery 如何获取月份列表javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16909553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:01:45  来源:igfitidea点击:

How to get list of months javascript

javascriptjqueryhtml

提问by Imesh Chandrasiri

I'm trying to get the list of months in javascript! how can I get this done by using only javascript!

我正在尝试获取 javascript 中的月份列表!我怎样才能通过只使用 javascript 来完成这项工作!

Thank you!

谢谢!

回答by Rong Nguyen

As far as I know, you can only get the array by hard-coding it.

据我所知,您只能通过硬编码来获取数组。

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

Or you can use some javascript library which has this list hard-coded.

或者您可以使用一些对此列表进行硬编码的 javascript 库。

回答by Andbdrew

Since you should be using momentto deal with dates anyway, you might as well use it here too! ;)

既然您无论如何都应该使用moment来处理日期,那么您也可以在这里使用它!;)

moment.months()or moment.monthsShort()(both added in 2.3.0):

moment.months()moment.monthsShort()(均在 2.3.0 中添加):

const moment = require('moment');
moment.locale('en'); // sets words language (optional if current locale is to be used)
moment.months() // returns a list of months in the current locale (January, February, etc.)
moment.monthsShort() // returns abbreviated month names (Jan, Feb, etc.)

回答by kuldeep raj

Please use this one:

请使用这个:

If you want to get the current month:

如果要获取当前月份:

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;
}

If you want to get all months:

如果您想获得所有月份:

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