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
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
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
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
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:
If you want to know more about javascript date and time methods, you may like following date and time methods:
You may like
- javaScript Get Current Year 2 and 4 Digit - Example
- Get Current Date Time javaScript
- JavaScript: Date setUTCDate() Method
- Set UTC Month In javaScript
- setUTCFullYear() Method JavaScript With Examples
- javascript date setUTCHours() Method With Examples
- JavaScript: d.setUTCMinutes() Method e.g.
- setUTCSecond() Method By JavaScript
- javaScript Date set UTC milliseconds
- setUTCSecond() Method By JavaScript
- JavaScript: Date.getUTCDate() Method
- getUTCmonth Method javaScript With Example
- Digital Clock with date in javaScript
- JavaScript: Set Date Methods
- JavaScript Get Date Methods
If you have any questions or thoughts to share, use the comment form below to reach us.