Tuesday 6 September 2016

How to get Month Name in JQuery?

In this post, I will explain how to get Month Name in JQuery?.

If you want to get the 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.

<!DOCTYPE html>
<html  lang="">
<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>

0 comments:

Post a Comment

Please do not enter any spam link in the message box.