javascript 从给定的月份名称中获取月份编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13178069/
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
Getting the month number from a given month name
提问by user1746560
I'm trying to find the corresponding month number from a given month name. What the easiest way of achieving this is?
我试图从给定的月份名称中找到相应的月份编号。实现这一目标的最简单方法是什么?
回答by Henrik Aasted S?rensen
There is not really a need for jQuery in this case.
在这种情况下,实际上并不需要 jQuery。
JavaScript along these lines might help:
这些方面的 JavaScript 可能会有所帮助:
var monthNumber = ["january", "february", "march"].indexOf(monthname.toLowerCase()) + 1;
Expand the array with all months to make it fully functional.
扩展所有月份的阵列以使其功能齐全。
回答by CompanyDroneFromSector7G
Simpler method to give a value from 0 (January) to 11 (December):
给出从 0(一月)到 11(十二月)的值的更简单方法:
var monthString = 'December';
var dat = new Date('1 ' + monthString + ' 1999');
alert(dat.getMonth());
回答by RobG
An alternative to Array.prototype.indexOf
is an object:
一个替代Array.prototype.indexOf
对象是:
var months = {jan: 0, feb:1, mar:2 ...};
var monthName = 'January';
var monthNum = months[monthName.substring(0,3).toLowerCase()];
The advantage is that you can accept various forms of the month name such as Jan, jan, JAN, January, JANUARY, etc.
优点是可以接受各种形式的月份名称,如Jan、jan、JAN、January、JANUARY等。
Oh, the above assumes you want month number as for a javascript Date constructor input. To get calendar month number, just increase the values by 1.
哦,上面假设您想要月份数作为 javascript Date 构造函数输入。要获得日历月数,只需将值增加 1。
回答by RDSAGAR
You don't need JQuery because this can be solved by javascript easily.
您不需要 JQuery,因为这可以通过 javascript 轻松解决。
Use Key Value pair like following
使用如下所示的键值对
var months = {jan:1, feb:2, mar:3, apr:4, may:5, jun:6};
You can get month number easily by calling month name like following
您可以通过调用月份名称轻松获得月份编号,如下所示
months.jan // will return 1 to the caller
or
或者
alert(months.feb); //will shows 2 in message box;
回答by rtpHarry
Depending on where this information is coming from you might be interested in using a library called DateJS:
根据此信息的来源,您可能对使用名为 DateJS 的库感兴趣:
It doesn't use jquery but it is good at parsing dates. If you look in the downloads section it also contains 150+ internationalised libraries if you need to handle foreign language input.
它不使用 jquery,但它擅长解析日期。如果您查看下载部分,如果您需要处理外语输入,它还包含 150 多个国际化库。
回答by Haagenti
The way i would do it is like this
我会这样做的方式是这样的
Make an array with all months;
用所有月份制作一个数组;
var data=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
//Lets say you get this back from your get month function. This should return january, because that is the first month..
var numberOfMonth = 1;
//An array starts with the index 0, so you could do minus 1 to get the correct one
alert data[numberOfMonth - 1]);