JavaScript Get Month 2 Digits From Date

JavaScript Get Month 2 Digits From Date

javascript get month 2 digits. In this tutorial, we will learn how to get a month in 2 digits with examples.

Before we get month 2 digits javascript, get current month 2 digits in javascript, javascript get month 2 digits from date, and get current month in 2 digits and year in 4 digits javascript. We must know the javascript method getMonth() , which is used to return the month of the specified date.

JavaScript date.getMonth() Method

The javascript getMonth() method, which is used to get the month of the specified date according to local time. It will return month from dates between 1 and 11.

Note: Jan is 0, Feb is 1. March is 2 and so on.

Syntax

Date.getMonth();

Here we will create a javascript function, which is used to convert month in 2 digits. In this function, will pass month and it will return month in 2 digits.

    function month2digits(month)
    { 
        return (month < 10 ? '0' : '') + month;
    }

Examples

Here we will take an example, javascript get month number, javascript get month 2 digits in javascript, javascript get current month 2 digits and get month 2 digits from date.

1. javascript get current month number

Let’s take a first example of get the current month number in javascript:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get month number</title>
</head>
<body>
  <script type = "text/javascript">
    var date = new Date(); // date object
    var getMonthNumber = date.getMonth()+1; // javascript current get month number
    document.write( "javascript get month number is :- " + getMonthNumber ); 
  </script>  
</body>
</html>

Result of the above javascript code is

javascript get month number

2. javascript getmonth 2 digits

Now we take second example for getting month in 2 digits javascript is:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get month 2 digits</title>
</head>
<body>
  <script type = "text/javascript">
    function month2digits(month)
    { 
        return (month < 10 ? '0' : '') + month;
    }
    var date = new Date(); // date object
    var month2digits = month2digits(date.getMonth()+1); // get month in two digits
    document.write( "javascript get month in 2 digits :- " + month2digits ); 
  </script>  
</body>
</html>

Result of the above javascript code is

javascript get month 2 digits

3. JavaScript Get 2 digits of Current Month

Now, we will take an example to get the current month in 2 digits is:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get current month 2 digits</title>
</head>
<body>
  <script type = "text/javascript">
    function month2digits(month)
    { 
        return (month < 10 ? '0' : '') + month;
    }
    var date = new Date(); // date object
    var month2digits = month2digits(date.getMonth()+1); // get current month in two digits javascript
    document.write( "javascript get current month 2 digits :- " + month2digits ); 
  </script>  
</body>
</html>

Result of the above javascript code is

javascript get current month 2 digits

4. Get Month 2 digits From date javascript

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get month 2 digits from date js</title>
</head>
<body>
  <script type = "text/javascript">
    function month2digits(month)
    { 
        return (month < 10 ? '0' : '') + month;
    }
    var date = new Date("Jan 26 2019 10:32:18"); // date object
    var month2digits = month2digits(date.getMonth()+1); // get month in two digits from data javascriopt
    document.write( "javascript get month 2 digits from date :- " + month2digits ); 
  </script>  
</body>
</html>

Result of the above javascript code is

javascript get month 2 digits from date

5. Get Current Month and Year in javascript

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get Current Month and Year in javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function month2digits(month)
    { 
        return (month < 10 ? '0' : '') + month;
    }
    var date = new Date(); // date object
    var month2digits = month2digits(date.getMonth()+1); // get month in two digits javascript
    var getCurrentYear = date.getFullYear(); // get current year javascript
    document.write( "javascript get current month 2 digits is :- " + month2digits ); 
    document.write( "<br>" ); 
    document.write( "javascript get current year is :- " + getCurrentYear ); 
  </script>  
</body>
</html>

Result of the above code is:

get current month and year in javascript

If you want to know more about javascript date and time methods, you may like following date and time methods:

You may like

  1. javaScript Get Current Year 2 and 4 Digit - Example
  2. Get Current Date Time javaScript
  3. JavaScript: Date setUTCDate() Method
  4. Set UTC Month In javaScript
  5. setUTCFullYear() Method JavaScript With Examples
  6. javascript date setUTCHours() Method With Examples
  7. JavaScript: d.setUTCMinutes() Method e.g.
  8. setUTCSecond() Method By JavaScript
  9. javaScript Date set UTC milliseconds
  10. setUTCSecond() Method By JavaScript
  11. JavaScript: Date.getUTCDate() Method
  12. getUTCmonth Method javaScript With Example
  13. Digital Clock with date in javaScript
  14. JavaScript: Set Date Methods
  15. JavaScript Get Date Methods

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *