JavaScript Get Month 2 Digits – Example

JavaScript Get Month 2 Digits – Example

javascript get month 2 digit. 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 and javascript get month 2 digits from date, we must know the javascript method getMonth() , which is used to return the month of the specified date.

JavaScript 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, we will pass month and it will return the 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 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 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. get month 2 digits in javascript

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 month in 2 digits :- " + month2digits ); 

  </script>  
</body>
</html>

Result of the above javascript code is

javascript get month 2 digits

3. javascript get current month 2 digits

Next, 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 month in two digits
    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. javascript get month 2 digits from date

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get month 2 digits from date</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
    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
    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.

Leave a Reply

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