JavaScript Get Current Year 2, 4 Digit

To get 2, 4 digits of current year in JavaScript. Through this tutorial, i am going to show you how to get current year 2 (two), 4(four) digits in javaScript witht help of examples.

JavaScript Get Current Year 2, 4 Digit

The getFullYear() method 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 of getFullYear() method; as shown below:

Date.getFullYear();

In this tutorial, i will take some examples using javaScript getFullYear() and substring() method to get current year in 2, 4 digits. Let’s see the following examples; as shown below:

  • 1 – JavaScript get current year in 4 digit
  • 2 – To get only last 2 digits of current year in javaScript
  • 3 – To get current year from date in javaScript
  • 4 – To get current year and month in javascript

1 – JavaScript get current year in 4 digit

Let’s see the following example for how to get current year in digits in javascript; as shown below:

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

Output of the above code is:-

javascript get current year 4 digit

2 – To get only last 2 digits of current year in javaScript

Let’s see the second example for how to get only last 2 digits of year in javascript; as shown below:

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

Output of the above code is:-

javascript get current year 2 digit dd/mm/yyyy to mm/dd/yyyy

3 – To get current year from date in javaScript

Let’s see the third example for get current year from date in javascript; as shown below:

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

Output of the above code is:-

javascript get 4 digit year from date

4 – To get current year and month in javascript

Let’s see the last example for To get current year and month in javascript; as shown below:

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

Output of the above code is:-

get current year and month in javascript

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

Be First to Comment

Leave a Reply

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