4

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

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

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
karth
  • 635
  • 3
  • 13
  • 25
  • See this SO question/answer: http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript – dacwe Sep 21 '11 at 09:29
  • hey @dacwe, I went through the question, I was looking for something more direct (If it exists), but it should work thou. – karth Sep 21 '11 at 12:01

3 Answers3

22

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.

Community
  • 1
  • 1
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • You're duplicating a lot of functionality here though, datepicker already has all this sort of functionality built in. – Dave Sep 21 '11 at 17:35
7

Edit: I guess getMonthName doesn't work?

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

var monthName = $.datepicker.formatDate('MM', $("#datepicker").datepicker('getDate'));
Dave
  • 11,499
  • 5
  • 34
  • 46
  • err. I hit it on Firebug's console and it says ".getMonthName is not a function". Could you please be little more specific. – karth Sep 21 '11 at 11:56
2

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()]
Galadai
  • 77
  • 6