jQuery 获取月份名称而不是数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7497488/
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 23:55:52  来源:igfitidea点击:

Get month name instead of number

jqueryjquery-uijquery-ui-datepicker

提问by karthik

I am using the jQuery datepicker plugin. I can get the month number by

我正在使用 jQuery datepicker 插件。我可以通过

$("#datepicker").datepicker('getDate').getMonth()+1

How can I get the month name directly (i.e. without using a switch case)?

如何直接获取月份名称(即不使用 a switch case)?

回答by dacwe

If you want a simple solution this is it:

如果你想要一个简单的解决方案,就是这样:

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

var selectedMonthName = months[$("#datepicker").datepicker('getDate').getMonth()];


A more complicated but more customizable way would be to use a formatter (my comment). Then see this question and answer.

更复杂但更可定制的方法是使用格式化程序(我的评论)。然后看这个问答

回答by Dave

Edit: I guess getMonthName doesn't work?

编辑:我猜 getMonthName 不起作用?

Well really the 'right' way to do this is to use formatDate: http://docs.jquery.com/UI/Datepicker/formatDate

那么真正的“正确”方法是使用 formatDate:http: //docs.jquery.com/UI/Datepicker/formatDate

var monthName = $.datepicker.formatDate('MM', $("#datepicker").datepicker('getDate'));

回答by Galadai

This is a very old thread but hey, improvements are always welcome (I hope!)

这是一个非常古老的线程,但嘿,总是欢迎改进(我希望!)

This works well if you have optional languages set.

如果您设置了可选语言,这很有效。

$( "#datepicker" ).datepicker( "option", "monthNames")[$("#datepicker").datepicker('getDate').getMonth()]