How to Get Month Name from Month Number in Jquery Moment Js

May 17, 2024 | jQuery


Hello Dev,

This tutorial is centered on obtaining the month name from its corresponding number using Moment.js. We'll delve into how to achieve this using jQuery and Moment.js. Specifically, we'll explore the implementation of retrieving the month name from its number using the format() method in Moment.js with the 'MMMM' parameter.

Read Also: How to Check Duplicate Values in Array PHP?
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Get Month Name from Month Number in Jquery Moment Js -- ItErrorSolution.com</title>
    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- Moment.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
</head>
<body>
<div id="monthName"></div>
<div id="monthName1"></div>

<script>
$(document).ready(function() {
    // Get the current month number (0-based index)
    var currentMonthNumber = new Date().getMonth();

    // Convert the month number to its full name using Moment.js
    var monthName = moment(currentMonthNumber + 1, "MM").format('MMMM');

    // Display the month name in the HTML element
    $('#monthName').text(monthName);
});
</script>
<script type="text/javascript">
  
    var startDateTime = '26-06-2024'
    var monthName = moment(startDateTime, "DD-MM-YYYY")
                        .format('MMMM');
  
    $('#monthName1').text(monthName);
  
</script>
</body>
</html>
Output:
May
June

Thank you for your encouragement! If you have any questions or need further assistance, feel free to ask. I'm here to help!



Tags :
#jQuery
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"