DEV Community

Tutsmake
Tutsmake

Posted on

javascript get month 2 digit

if you are searching like javascript get month 2 digit from given date or current date. So this tutorial will help you find the month two digit from the given date or current date.

How do I get Month and Date of JavaScript in 2 digit format

Now we take an 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>

You can found more like javascript get month 2 digit, javascript get year 2, 4 digit ==> https://www.tutsmake.com/javascript-get-month-2-digits-example/

Top comments (2)

Collapse
 
patryktech profile image
Patryk

Just add left-pad as a dependency. What could go wrong?

Collapse
 
7qruzer profile image
Kumar Gaurav

return ('0'+month).slice(-2)