Get month name in jQuery

If you want to get month name instead of month number, simply get the month number and convert it into month name with the help of given function. Below example demonstrate how you get current month name in jQuery.

$('.btnGetMonthName').click( function() {
  alert(GetMonthName(3)); //Display March
});

function GetMonthName(monthNumber) {
  var months = ['January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December'];
  return months[monthNumber-1];
}

To get current month number in jQuery, Click Here.

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {

      $('.btnGetMonthName').click(function () {
        var monthNumber = $('.txtMonthNumber').val();
        var monthName = GetMonthName(monthNumber);
        if(monthName != undefined)
          alert(monthName);
        else
          alert("Enter valid month number");
      });
    });

    function GetMonthName(monthNumber) {
      var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
      return months[monthNumber - 1];
    }
  </script>
</head>
<body>
   <div>
    Enter month number: <input type="text" class="txtMonthNumber" value="3" />
    <button class="btnGetMonthName">Get Month Name</button>
  </div>
</body>
</html>
DISPLAY
Enter month number:

3 comments :

  1. after entering the invalid month code it is showing the undefined. Show the proper message as "Enter the valid month code."

    ReplyDelete
    Replies
    1. Hi Bishunjay,
      Validation added in above code.

      Delete