Javascript 在 JS 中将月份名称转换为月份编号的最简单方法?(一月 = 01)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13566552/
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
Easiest way to convert month name to month number in JS ? (Jan = 01)
提问by l2aelba
Just want to covert Janto 01(date format)
只想将Jan 转换为01(日期格式)
I can use array()but looking for another way...
我可以使用array()但正在寻找另一种方式...
Any suggestion?
有什么建议吗?
回答by Aaron Romine
Just for fun I did this:
只是为了好玩,我这样做了:
function getMonthFromString(mon){
return new Date(Date.parse(mon +" 1, 2012")).getMonth()+1
}
Bonus: it also supports full month names :-D Or the new improved version that simply returns -1 - change it to throw the exception if you want (instead of returning -1):
额外奖励:它还支持完整的月份名称 :-D 或者只是返回 -1 的新改进版本 - 如果需要,将其更改为抛出异常(而不是返回 -1):
function getMonthFromString(mon){
var d = Date.parse(mon + "1, 2012");
if(!isNaN(d)){
return new Date(d).getMonth() + 1;
}
return -1;
}
Sry for all the edits - getting ahead of myself
Sry 所有的编辑 - 超越自己
回答by Alex K.
Another way;
其它的办法;
alert( "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf("Jun") / 3 + 1 );
回答by Paul S.
If you don't want an array then how about an object?
如果你不想要一个数组,那么一个对象呢?
var months = {
'Jan' : '01',
'Feb' : '02',
'Mar' : '03',
'Apr' : '04',
'May' : '05',
'Jun' : '06',
'Jul' : '07',
'Aug' : '08',
'Sep' : '09',
'Oct' : '10',
'Nov' : '11',
'Dec' : '12'
}
回答by Vikas Hardia
One more way to do the same
另一种方法来做同样的事情
month1 = month1.toLowerCase();
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
month1 = months.indexOf(month1);
回答by chinupson
If you are using moment.js:
如果您使用的是 moment.js:
moment().month("Jan").format("M");
回答by Akhil Sekharan
I usually used to make a function:
我通常用来做一个函数:
function getMonth(monthStr){
return new Date(monthStr+'-1-01').getMonth()+1
}
And call it like :
并称之为:
getMonth('Jan');
getMonth('Feb');
getMonth('Dec');
回答by Muzafar Hasan
function getMonthDays(MonthYear) {
var months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
var Value=MonthYear.split(" ");
var month = (months.indexOf(Value[0]) + 1);
return new Date(Value[1], month, 0).getDate();
}
console.log(getMonthDays("March 2011"));
回答by Arturo Romero Marquez
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
then just call monthNames[1] that will be Feb
然后只需调用将是二月的 monthNames[1]
So you can always make something like
所以你总是可以做一些像
monthNumber = "5";
jQuery('#element').text(monthNames[monthNumber])
回答by Chris Martin
Here is a modified version of the chosen answer:
这是所选答案的修改版本:
getMonth("Feb")
function getMonth(month) {
d = new Date().toString().split(" ")
d[1] = month
d = new Date(d.join(' ')).getMonth()+1
if(!isNaN(d)) {
return d
}
return -1;
}
回答by Erick Garcia
Here is another way :
这是另一种方式:
var currentMonth = 1
var months = ["ENE", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AGO", "SEP", "OCT", "NOV", "DIC"];
console.log(months[currentMonth - 1]);

