JavaScript Get Month in 2 Digit

JavaScript get month 2 digits; Through this tutorial, i am going to show you how to get month in 2 digits in javaScript with the help of examples.

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.

JavaScript date.getMonth() Method

Using the javaScript getMonth(); you can get month in 2 digits.

In this tutorial, i will take some example to get month in digits in javaScript; as shown below:

  • 1 – JavaScript Get Current Month Number
  • 2 – JavaScript Get Month in 2 Digit
  • 3 – JavaScript Get Current Month in 2 Digit
  • 4 – JavaScript Get Month in 2 Digit From Date

1 – JavaScript Get Current Month Number

Let’s see the example to get the current month number in javaScript using getMonth() method; as shown below:

<!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>

Output of the above code is:-

javascript get month number

2 – JavaScript Get Month in 2 Digit

Let’s see the following example to get month in 2 digits javascript; as shown below:

<!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>

Output of the above code is:-

javascript get month 2 digits

3 - JavaScript Get Current Month in 2 Digit

Let's see the following example for get current month in 2 digit in javaScript; as shown below:

<!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>

Output of the above code is:-

javascript get current month 2 digits

4 - JavaScript Get Month in 2 Digit From Date

Let's see the following javaScript example for get month in 2 digit from date; as shown below:

<!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>

Output of the above code is:-

javascript get month 2 digits from date

Recommended JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.