javascript get current year 2, 4 digit; In this tutorial, we will learn how to get current year two digits in javascript.
Before we get two digits of the current year, get four digits from date and get four digits of the current year, we know about the getFullYear() javascript method, which is used to get the year of the specified date.
JavaScript getFullYear() Method
The getFullYear() method of the javascript method, which is used to get the year of the specified date. It will return year to four digits for dates between the year 1000 and 9999.
Syntax
Date.getFullYear();
Examples
Here we will take an example like, how to get current year 2 digit, how to get the last two digits of year javascript, how to get current year 4 digits javascript and How to get javascript get 4 digit year from date.
1. get current year 4 digit javascript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>javascript get current year 4 digit</title> </head> <body> <script type = "text/javascript"> var date = new Date(); // date object var getYear = date.getFullYear(); // get current year document.write( "javascript get current year 4 digit is :- " + getYear ); </script> </body> </html>
Result of the above code is:
2. javascript get current year 2 digit
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>javascript get current year 2 digit</title> </head> <body> dd/mm/yyyy to mm/dd/yyyy <script type = "text/javascript"> var date = new Date(); // date object var getYear = date.getFullYear(); // get current year var getTwodigitYear = getYear.toString().substring(2); // get last two digits from year document.write( "javascript get current year 2 digit is :- " + getTwodigitYear ); </script> </body> </html>
Result of the above code is:
3. javascript get 4 digit year from date
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>javascript get 4 digit year from date</title> </head> <body> <script type = "text/javascript"> var date = new Date("Mon Nov 25 2019 22:37:25"); // date object var getYear = date.getFullYear(); // get current year document.write( "javascript get 4 digit year from date :- " + getYear ); </script> </body> </html>
Result of the above code is:
4. get current year and month in javascript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>get current year and month in javascript</title> </head> <body> <script type = "text/javascript"> function month2digits(month) { return (month < 10 ? '0' : '') + month; } var date = new Date(); // date object var getCurrentYear = date.getFullYear(); // get current year javascript document.write( "get current year in javascript is :- " + getCurrentYear ); document.write( "<br>" ); var month2digits = month2digits(date.getMonth()+1); // get month in two digits document.write( "javascript get current month 2 digits is :- " + month2digits ); </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
- 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.